Original post

So this must be a very stupid question since I can’t find the answer anywhere, and it doesn’t seem like they explain it in the book, but how come this works:

// Add command-line arguments to worklist.  func() { worklist <- os.Args[1:] }() 

But this doesn’t (without the function)

worklist <- os.Args[1:] 

Why do I need to spawn an anonymous goroutine/function in order to populate this channel?

I understand that this is the only way it works, I just don’t understand why.

submitted by /u/if_it_is_in_a
[link] [comments]