Nope, it's not safe. https://play.golang.org/p/pcPdKixphL
Run this program in a loop and it will panic eventually. On Wednesday, 14 December 2016 15:19:24 UTC+11, P Q wrote: > > func close_safe(c chan bool) { > select { > case <-c: > default: > close(c) > } > } > > Closing a channel onece more can be a controversial topic, sometimes it is > useuful. > I saw the code to close a closed channel. I thought the code looked nice > at first, but could become errernous. > > Let's assume that two gorutines will try to close the channel and the > channel has not been closed. At first, groutine 1 enters the default block. > At this time, gorutine schedule changes so that groutine 2 gets the > execution. and the groutine 2 enters the default block, and closes the > channel. schedule changes, gorutine 1 restores its execution and will panic > by closing the closed channel. > > Could this way happen? > > -- 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.