Closed channel or unclosed

Why second time geeting output from channel it appeared that channel closed: false?
Obviously, channel closed: false is equals to cnannel is open. But how it can be open if it has been allready closed?!

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

1 Like

Hi @cinematik,

isClosed is not correct here. The second value in a channel is if a value was returned.



Hi, @cinematik,

The second result from a receive operation is not whether or not the channel is closed but whether the receive was successful:, making it similar to when you lookup a result from a map: v, ok := a[x]


The close spec explains the second boolean result very well on receive operator.

After calling close , and after any previously sent values have been received, receive operations will return the zero value for the channel’s type without blocking. The multi-valued receive operation returns a received value along with an indication of whether the channel is closed.

That means you can use the second untyped boolean variable to check whether the channel closed or not.

If you get False from the receiver operation, it means the channel is closed not open. I recommend you to use ok name to make it more clear and go way.


ok is not a keyword, its just an identifier, that is convtionally used for booleans in the second return value to indicate success.


Right! wrong terminology, ok is not a go keyword. Thanks @NobbZ

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.