FAQ
Hello everybody,

I'm experimenting with channels to learn how it work and all that.. but
when i tries to do something in this example.. i didn't expect the result i
want.. for example, i want the string pass through the channel to be
string... for some reason it returned the pointer instead -


package main

import (
"fmt"
"time"
)

func reply(d time.Duration, c chan <- string){
     test := "vroom.. vroom.."
time.Sleep(d * time.Millisecond)
     c <- test
}
func main() {
tick := time.Tick(100 * time.Millisecond)
boom := time.After(500 * time.Millisecond)
     response := make(chan string)
     go reply(50, response)
  for {
select {
case <-tick:
fmt.Println("zoom.")
case <-boom:
fmt.Println("BOOM!")
return
case <-response:
fmt.Printf("%s", response)
default:
fmt.Println(" .")
time.Sleep(50 * time.Millisecond)
}
}
}


where did I went wrong here?
http://play.golang.org/p/GM3riYrq5S

thanks in advance,
Richard

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Kornel Maczyński at Aug 16, 2014 at 11:01 pm
    You're printing a channel, not a response. Try
    this: http://play.golang.org/p/hVQoY_9RY9

    Kornel

    On Saturday, August 16, 2014 11:55:46 PM UTC+1, Richard Moffitt wrote:

    Hello everybody,

    I'm experimenting with channels to learn how it work and all that.. but
    when i tries to do something in this example.. i didn't expect the result i
    want.. for example, i want the string pass through the channel to be
    string... for some reason it returned the pointer instead -


    package main

    import (
    "fmt"
    "time"
    )

    func reply(d time.Duration, c chan <- string){
    test := "vroom.. vroom.."
    time.Sleep(d * time.Millisecond)
    c <- test
    }
    func main() {
    tick := time.Tick(100 * time.Millisecond)
    boom := time.After(500 * time.Millisecond)
    response := make(chan string)
    go reply(50, response)
    for {
    select {
    case <-tick:
    fmt.Println("zoom.")
    case <-boom:
    fmt.Println("BOOM!")
    return
    case <-response:
    fmt.Printf("%s", response)
    default:
    fmt.Println(" .")
    time.Sleep(50 * time.Millisecond)
    }
    }
    }


    where did I went wrong here?
    http://play.golang.org/p/GM3riYrq5S

    thanks in advance,
    Richard
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 16, '14 at 10:55p
activeAug 16, '14 at 11:01p
posts2
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase