0

The blow code does not print anything even if main function is writing to channel. Is it because the goroutine running the anonymous function exited before channel could receive any value?

package main
import "fmt"

func main() {
  ch := make(chan int)
  go func () {for{fmt.Println(<-ch)}} ()
  ch <- 1
}
Vishwa
  • 196
  • 4
  • 15
  • You've only guaranteed that the value is received in the goroutine, nothing else. Your goroutine also never returns, so I'm not sure what you mean by "the anonymous function exited" – JimB Feb 07 '18 at 17:35
  • It is because the main retuned and thus the program ends. Even if the value is returned `fmt.Printx` involves a syscall which can lead to reschedule – leaf bebop Feb 07 '18 at 17:38
  • @JimB Other question/answers solved my problem. – Vishwa Feb 08 '18 at 12:06

0 Answers0