This is a creation in Article, where the information may have evolved or changed.
Overview
In order to communicate between the Goroutines, the Golang uses the channel channel. You can send data to the channel via a goroutines and then receive it from another goroutine
Usually we use make to create channel-----makes (chan valType, [size]).
Writechannel c<-
读取channel <-c
Classification
One No buffer
Create an unbuffered channel instance by ignoring the second parameter when make is created as follows:
Csdntest.gopackage mainimport ("FMT" "Time") Func main () {messages: = Make (Chan string) go func () {Messages <-"he Llo "} () Msg: = <-messages fmt. PRINTLN (MSG)}
It is important to note that: unbuffered channel only the receiver and the sender are ready, the channel to work properly, if we put msg: = <-messages This comment out, then messages <-"Hello" will be blocked.
Here is a conjecture: unbuffered buffer is only used when the pipe,pipe both sides of the IPC are open. "<-" is interpreted as opening the write-end, "---interpreted as open read"
Two buffers
Example:
Csdntest.gopackage mainimport ("FMT") func main () {messages: = Make (Chan string, 2) messages <-"Hello" messages <- "World" FMT. Println (<-msg) fmt. Println (<-MSG)}
Note: When creating buffered buffers, there is no need to have a prepared receiver to accept the data, the data can exist in the channel buffer, the buffer channel has the following characteristics:
1: The data is not full can be written, when the data is full and then write blocked.
2: The data is not empty can be read, when the data is empty reread blocked.
Through the above features can make a bold guess, the buffer is through the msg-queue to achieve.
There are buffers and unbuffered implementations from the ground up are different because they use different IPC.
Three non-blocking
The channel reads and writes are blocked under normal circumstances, but we can use a select with default to trigger non-blocking Channle. The select is also blocked, and the select itself becomes non-blocking when the default is added
As an example:
Csdntest.gopackage mainimport "FMT" Func main () {messages: = Make (Chan string) Signals: = Make (chan bool)//message and Signa LS is an unbuffered channel because the receiver and transmitter are not ready at the same time, so the channel cannot be used. All of the following select outputs the default contents of select {case msg: = <-messages:fmt. Println ("Received message", MSG) default:fmt. PRINTLN ("no Message Received")}msg: = "HI" SELECT {case Messages <-msg:fmt. PRINTLN ("Sent Message", msg) default:fmt. PRINTLN ("no message Sent")}select {case msg: = <-messages:fmt. Println ("Received message", msg) case SIG: = <-signals:fmt. PRINTLN ("Received signal", SIG) Default:fmt. PRINTLN ("No Activity")}}
Interested students to change the above unbuffered to have a buffer to try.
As for why select does not block the channel and itself after joining the default, you can go to the source file of Go source Select.go,-------------------can also read my next blog.
Use cases of four channel
4.1 Processing Timeout
c: = Make (chan string, 1) go func () {Time . Sleep (time. Second * 3) C <-"inner Goroutine" } () Select {Case res: = <-c: fmt. PRINTLN (res) case <-time. After (time. Second * 1): FMT. PRINTLN ("timeout") }
4.2 Channel Closed
When you close a channel, you cannot write data to the channel. Usually we use a value to return a value to receive the channel data, but at this point we can use two return values (VALEU,B:=<-C) to capture whether the channel turns off B value false or True.
When we are dealing with a task that needs to be ended, we can dispatch it in the following way
Jobs: = Make (chan int, ten) Done: = Make (chan bool) go func () {for {J ', more: = <-jobsif more {fmt. Println ("Received job", J)} else {fmt. Println ("Received all Jobs") did <-Truereturn}}} () for j: = 1; J <= 3; J + + {jobs <-jfmt. Println ("Sent Job", j)}close (jobs)
4.3 Data table-Tennis type of circulation
An example of a more well-known Golang official website:
Package Mainimport "FMT" func ping (pings chan<-string, msg string) { pings <-msg}func pong (pings <-chan stri Ng, pongs chan<-string) { msg: = <-pings pongs <-msg}func Main () { pings: = Make (chan string, 1)
pongs: = Make (chan string, 1) Ping (pings, "passed message") pong (pings, pongs) FMT. Println (<-pongs)}
Thank you for reading.