This is a creation in Article, where the information may have evolved or changed.
Go concurrency
With the Go keyword before a function call, this call is executed concurrently in a new goroutine.
Func Add (x, y int) { Z: = x + y fmt. Println (z)}func main () { for I: = 0; i < ten; i++ { go Add (i, i) }}
The Go program starts with the initialization of the main package and executes the main () function, and when the main () function returns, the program exits. and the program does not wait for
Other Goroutine (non-primary goroutine) end.
Concurrent communication
The Go language, which takes concurrent programming as the core advantage of language, provides a communication model, that is, message mechanism rather than shared memory as the means of communication. The message mechanism considers that each concurrent unit is self-contained, independent, and has its own variables, but these variables are not shared between different concurrent units. There is only one input and output for each concurrent unit , and that is the message. This has a bit of a process-like generalization , and each process will not be disturbed by other processes, it can only do its own work. Messages are communicated between different processes, and they do not share memory.
the message communication mechanism provided by the go language is called the channel"Do not communicate through shared memory, but should use communication to share memory." ”
Channel
The channel is the way that the go language communicates between the goroutine provided at the language level. Channel is type-dependent
Chan statement
var channame chan ElementType
such as:
var chint chan int //What an int type of Chanvar MCH Map[string] chan bool //Declare a map whose elements are Boolvar arrch [10]chan int//define A
Instance
Func Count (ch Chan int) { ch <-1 fmt. Println ("Counting")}func main () { CHS: = make ([]chan int, ten) for I: = 0; i <; i++ { chs[i] = Make (chan int) go Count (chs[i]) } for _, ch: = Range (CHS) { <-ch }}
Define channel use the built-in function make () directly:
CH: = make (chan int)
Channel basic usage Write
<span style= "White-space:pre" ></span>ch < 1
writing data to the channel usually causes the program to block until another goroutine reads the data from the channel
Read out
I: = < CH
If CH has no data it will cause the program to block until there is data written to Ch
However,The blocking of CH can be flexibly developed by using the buffer mechanism of CH and select.
buffering mechanism The buffered channel is suitable for scenarios where large amounts of data need to be transmitted continuously, defining a buffered channel, simply passing the capacity into the second parameter of make
c: = make (chan int, 1024x768) //Create a plastic chan with a size of 1024, the write will not block until the buffer is full
channel is transitive.
unidirectional channel one-way channel can only be used to send or receive data. Statement
var ch1 chan Int//CH1 is a normal channel, not one-way var ch2 chan<-float64//CH2 is unidirectional channel, only used to write float64 data var CH3 <-chan INT// CH3 is a one-way channel that is used only to read int data
Initialization Channel can convert between one-way channel and two-way channel, the channel itself is the native type of go, so it can be passed, or it can be type-converted
CH4: = make (chan int) Ch5: = <-chan int (CH4)//CH5 is a one-way read channelch6: = chan<-int (CH4)//CH6 is a one-way write channel
Close channel
Directly with close
Close (CH)
Determines if the channel is closed, can be returned with multiple values, if the second bool return value is FALSE indicates that CH has been closed
X, OK: = <-ch
Select
Used to monitor the IO operation of a series of file handles, and once one of the handles has an IO operation, it is returned Go supports select at the language level The code structure is roughly as follows:
Select {<span style= "white-space:pre" ></span>case <-chan1:<span style= "White-space:pre" ></ span>//if Chan1 successfully read the data, the case processing statement <span style= "White-space:pre" ></span>case chan2 <-1:<span Style= "White-space:pre" ></span>//if the data is successfully written to chan2, the case processing statement is <span style= "White-space:pre" ></ Span>default:<span style= "White-space:pre" ></span>//if none of the above is successful, go to the default process}
Each of these must be a file handle operation Combine for to enable loop detection
CH: = Make (chan int, 1) for {<span style= "White-space:pre" ></span>select {<span style= "White-space:pre" ></span>case ch <-0:<span style= "white-space:pre" ></span>case ch <-1:<span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>i: = <-ch<span style=" White-space:pre "></span>fmt. Println ("Value Received:", I)}