// Cakes have a private channel to let us know when they're done.
type Cake struct {
BakingStatus chan string
}
// Oven instantly notifies every cake that it is done.
func Oven(cakes chan *Cake) {
for c := range cakes {
c.BakingStatus <- "Done"
}
}
func main() {
cakes := make(chan *Cake)
go Oven(cakes)
myCake := &Cake{}
cakes <- myCake // Put the cake in the oven.
<-myCake.BakingStatus // Wait for cake to be done.
fmt.Println(myCake)
}
(This example is silly and contrived because I've pared it down to the absolute minimum for the sake of this post.)
In my head, this should work by the following steps:
- The Oven goroutine sleeps until something is sent on
cakes. myCakeis sent tocakes.- The Oven goroutine receives the cake.
- The main goroutine sleeps until something is sent on
myCake.BakingStatus. Ovensends a value tomyCake.BakingStatusand goes back to sleep.- The main goroutine receives
myCakeand prints it.
The only problem I can imagine is that there's a moment between the main goroutine sending and the Oven goroutine receiving the cake where they're both asleep. But then what is the solution?
CodePudding user response:
The "BakingStatus" is not initialized. Writing to and reading from a nil channel blocks forever.
myCake := &Cake{
BakingStatus: make(chan string),
}
