Your program exits because the channel is buffered, so the main goroutine 
exits immediately once it has sent the value to the channel. Once the main 
goroutine returns, the program will exit.

On Saturday, 6 August 2016 08:38:54 UTC+10, Jon Strauss wrote:
>
> Hello,
> I have the following code:
>
> package main
>
> import "fmt"
> import "encoding/hex"
> import "os"
>
> func main() {
>        var bytes []byte
>        var channel chan []byte
>
>        file,_ := os.OpenFile("/dev/random", os.O_RDONLY, 0)
>        bytes = make([]byte,20)
>        _, _ = file.Read(bytes)
>
>        channel = make(chan []byte,1)
>        go worker(channel)
>        channel <- bytes
> }
>
> func worker(channel chan []byte) {
>        var dest string
>
>        bytes := <-channel
>        dest = hex.EncodeToString(bytes)
>        fmt.Println(dest)
>
>
> }
>
>
> Instead of printing a hex string the program simply exits. What am I doing 
> wrong?
> -William
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to