I know we can read from buffered close channels. So, I feel very confident that we can do from unbuffered as well. However, I am not able to test it out.
func test(c chan int) {
time.Sleep(10 * time.Millisecond)
close(c)
fmt.Println(<-c)
}
func main() {
c := make(chan int)
go test(c)
c <- 5
}
Output
panic: send on closed channel
goroutine 1 [running]:
main.main()
/Users/tanmay.jhazomato.com/Projects/search-service/practice.go:17 +0x85
Process finished with the exit code 2
As far as I know, in this code. First c<-5
should be executed before channel getting closed by test go routine
. However, that's not the case. Can somebody tell me when cooperative scheduling
is not working here? Why is it that the test go routine
is executing first?