essential-go icon indicating copy to clipboard operation
essential-go copied to clipboard

error at "closeing channels"

Open samuelyao314 opened this issue 5 years ago • 2 comments

samuelyao314 avatar Dec 11 '18 06:12 samuelyao314

there is an error at Chapter 12. example:

ch := make(chan int) go func() { ch <- 1 close(ch) }() v, isClosed := <-ch fmt.Printf("received %d, is channel closed: %v\n", v, isClosed)

here, name "isClosed" doesn't mean "channel is closed", means "is ok to receive an value from channel"

samuelyao314 avatar Dec 11 '18 06:12 samuelyao314

so, the collect code is

ch := make(chan int) go func() { ch <- 1 close(ch) }() v, ok := <-ch if !ok { fmt.Printf("received %d, channel closed\n", v) }

samuelyao314 avatar Dec 11 '18 06:12 samuelyao314