This is a creation in Article, where the information may have evolved or changed.
Channel
Repeat closing channel
CH: = Make (chan bool) close (CH)//This will panic, channel cannot close two times
when read, the channel closes the channel early.
Close (ch) I: = <-ch//Not panic, but I read 0 value (""/false/nil)
write data to a channel that has been closed
CH: = Make (Chan string) Close (CH) ch <-"Test"//Will 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
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")}