This is a creation in Article, where the information may have evolved or changed.
Channel
Sample1 Close twice
CH: = Make (chan bool) close (CH) //This will panic, channel cannot close two times
The channel was closed early when it was read.
CH: = Make (Chan string) close (ch) I: = <-CH//will not panic, I read to the value is empty "", if the channel is bool, then read to False
Write data to a channel that has been closed HTTP://PLAY.GOLANG.ORG/P/VL5D5TKFL7
CH: = Make (Chan string) Close (CH) ch <-"good"//Panic
Determine if the channel is close
I, OK: = <-chif OK { println (i)} else { println ("Channel Closed")}
For Loop Read Channel
For i: = range ch {//ch is closed, the For loop automatically ends println (i)}
Prevent read Timeouts
Select {Case <-time. After (time. second*2): println ("read Channel timeout") Case I: = <-ch: println (i)}
Prevent write Timeouts HTTP://PLAY.GOLANG.ORG/P/AODFPNWDMJ
In fact, the read timeout is much like the select {case <-time. After (time. Second): println ("Write channel timeout") Case ch <-"Hello": println ("Write OK")}