I have a struct that holds a channel that is not initialized.
When I write into it, the routine is blocking as expected but the reader is never notified that something is in the pipe.
I am surprised that there is no error and I am wondering what is doing Go.
In the example bellow, neither the message pushed
nor got it
are printed. (Uncomment the intialization and it will work like a charm)
type MyStruct struct {
over chan bool
}
func main() {
nonInitialized := &MyStruct{}
// nonInitialized.over = make(chan bool)
go func() {
for i := 0; i < 10; i++ {
select {
case <-nonInitialized.over:
fmt.Println("got it")
default:
// proceed
}
fmt.Println("do some stuff")
time.Sleep(10 * time.Millisecond)
}
panic("took too long")
}()
// push on the non initialized channel
fmt.Println("pushing")
nonInitialized.over <- true
fmt.Println("pushed")
}
Here is the playground https://play.golang.org/p/76zrCuoeoh
(I know I should initialize the channel, this is not the purpose of the question, I want to know what is happening in Go with non initialized channels.)