This is a creation in Article, where the information may have evolved or changed.
There is a significant difference between buffer and unbuffered Golang channel.
I was naïve to think that there is a buffer and no buffer difference just unbuffered is the default buffer of 1 buffer
In fact, it's completely wrong, and there's a big difference between unbuffered and buffered channel.
That's one of the synchronizations that is a non-synchronous
What do you say? Like what
C1:=make (chan int) unbuffered
C2:=make (Chan int,1) has a cushion
C1<-1
Unbuffered not only to the C1 Channel 1 but always have to have other Ctrip <-C1 took over this parameter, then c1<-1 will continue, otherwise it has been blocked
The c2<-1 is not blocked, because the buffer size is 1 only when the second value is put in the first one has not been taken away, this time will be blocked.
Make a metaphor.
No buffer is a messenger to your door to send letters, you are not at home he does not go, you must take the letter, he will go.
No buffer guarantee to get your hands on the letter.
There is a buffer is a messenger to your home still to your home mailbox turned away, unless your mailbox is full he must wait for the mailbox empty.
There is a buffer guarantee to enter your home mailbox.
See the test code
There's a cushion.
1Package Main23 Import"Fmt"45var c = Make (chanInt1) 6 7 func f () { 9 c <- Span style= "color: #800000;" > '
"c" <-c<-C,FMT. Println (" external call ")
Non-buffered
//PpPackage Mainimport ("Fmt") func Writeroutine (Test_chan Chanint, valueInt) {Test_chan <-Value}func Readroutine (Test_chan Chaninttest_chan Span style= "color: #0000ff;" >return}func Main () {c: = Make (chan int100 ////go Writeroutine (c, x) //writeroutine (c, x) // go Readroutine (c) //go Readroutine (c) //writeroutine (c, x) go Writeroutine (c, x)
Go Writeroutine (c, x) Readroutine (c) FMT. PRINTLN (x)}
Can be commented out in part of the code, see the effect, and then experience the next