As others pointed out, you need to use a lock if you want to close a 
channel from multiple, possibly concurrent, code locations. 

Here is an example of how I handle such situations:

https://github.com/glycerine/idem/blob/master/halter.go#L11

Feel free to use that library, or parts of it.  In short, if you want an 
idempotent close, you have to replace close() with your own Close().

Meta comment: it was, in my view, a poor design decision in Go (not to 
allow multiple closes of a channel). For backwards compatibility reasons we 
are stuck with it now.  Its a wart/minor annoyance in the grand scheme of 
things. Fortunately, the workarounds, while elaborate and slow, are very 
rarely on hot code paths.

On Monday, November 6, 2017 at 6:59:51 PM UTC-6, 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?
>

-- 
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.

Reply via email to