If you're using it as a signal to trigger only on close and not sending
any data, you should use chan struct{}, the reason for this is is that
the empty struct consumes zero storage
<https://dave.cheney.net/2014/03/25/the-empty-struct>.
Also there is a multi-valued assignment form of the receive operator
<https://golang.org/ref/spec#Receive_operator>, which reports whether a
received value was sent before the channel was closed.
Dave Cheney's article curious channels
<https://dave.cheney.net/2013/04/30/curious-channels> is also relate so
worth a read.
Regards
Steve
On 07/11/2017 00:59, Albert Tedja wrote:
So, I just found out that closed channels always yield the zero value.
That means, a closed channel inside a select statement seems to always
be guaranteed to be executed.
Since closing an already closed channel creates a panic, does this
mean then I can do this to make sure that the channel is closed only once?
|
select{
case<-closedchan:
default:
close(closedchan)
}
|
Golang playground: https://play.golang.org/p/LSrTh0HC2K
It seems to work. Just want to confirm here before start doing this
everywhere in my code.
--
You received this message because you are subscribed to the Google
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-nuts+unsubscr...@googlegroups.com
<mailto:golang-nuts+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.