go - Why is my goroutine not executed? -


i'm learning go , wanted try goroutines , channels.

here's code:

package main import "fmt" func main(){  messages := make(chan string,3)  messages <- "one" messages <- "two" messages <- "three"  go func(m *chan string) {     fmt.println("entering goroutine...")     {         fmt.println(<- *m)     } }(&messages)  fmt.println("done!") } 

and here's result:

done! 

i don't understand why goroutine never executed. "entering goroutine" not printed , don't have error message.

the fact goroutine starts, ended before doing because program stop right after printing done!: execution of goroutines independant of main program, stopped @ same program. basically, need process make program wait them. channel waiting number of messages, sync.waitgroup, or other tricks.

you should read excellent post concurrency in go in golang blog.


Comments