This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("io" "FMT" "Log" "NET") const listenaddr= "0.0.0.0:4000" var partner=make (chan io. Readwritecloser) func match (c io. Readwritecloser) {fmt. Fprintln (c, "Wait for partner ...") select{case partner<-c://do nothingcase P: = <-partner:chat (c,p)}}func chat (A, b Io. Readwritecloser) {fmt. Fprintln (A, "Yes,i got a partner ...") fmt. Fprintln (b, "Yes,i got a partner ...") go IO. Copy (b) Io. Copy (B,a)}func main () {l,err:= net. Listen ("TCP", listenaddr) if Err!=nil {log. Fatal (Err)}for {c,err: = l.accept () if Err!=nil {log. Fatal (Err)}go match (c)}}
A Select blocks until one of its cases can run and then it executes the case. It chooses one at random if multiple is ready.
http://tour.golang.org/#64
By default, sends and receives blocks until the other side are ready. This allows Goroutines to synchronize without explicit locks or condition variables.
var partner=make (Chan io. readwritecloser,1)
Buffed channels only full block.