1. Basic concepts of Go language channel
Generate background
When communicating between threads, there will be a state problem due to the competition for resources. In order to ensure the correctness of data exchange, mutexes must be used to lock the memory. The concurrency model of go language is CSP, which advocates sharing memory through communication rather than sharing memory. The channel happens to meet this requirement.
How to work
channel
Similar to a queue, it meets the first-in-first-out rules and strictly ensures the order of sending and receiving data. Each channel can only pass through fixed types of data. If the channel transmits large structures and strings, the corresponding pointer can be passed in, saving space as much as possible.
2. Channel usage syntax
1. Channel declaration and initialization
//Define a channel object to use, where int can be replaced with the type you need var a chan int //Initialize a channel with only one position (the first parameter represents the channel type, and the second parameter represents how many positions the channel has) //After the location is full, new data will not be stored (blocked) a = make(chan int,1)
2. Put the data into the channel
- Use the operator to retrieve data using the <- operator right is the input variable, and the operator left is the channel that represents the data flowing into the channel
The code is as follows:
// Declare a channel var a chan int a <- 5
3. Extract data from the channel
- The operator is also used to retrieve data <- The operator is the channel on the right, and the operator is the variable on the left.
The code is as follows:
//Declare a channel type var a chan int ("Uninitialized channel", a) a = make(chan int) // (1) go func(a chan int) { // defer () for { x := <-a ("Data received:", x) } }(a)
4. Close the channel close
If the channel is repeatedly closed or closed a channel without initialization, an error will be thrown
close(a)//aFor the channel to be closed
The code for closing the channel at one time in the concurrent function is as follows:
// Mutex objectvar once //Concurrent functions//The purpose of this function is to multiply the data in channel a by 10 and send it to channel bfunc f2(a <-chan int, b chan<- int) { defer () for { x, ok := <-a if !ok { break } (x) b <- x * 10 } // Make sure channel b is closed only once (func() { close(b) }) }
3. Analysis of the status of single channels and channels
1. Single-item output channel
var b <-chan int
2. Single input channel
var b chan<- int
Example function:
//Single-item channels are generally used as function parameters, as a specification to prevent channels from being mixed//The function completed by this function is to multiply the data in a by 10 and put it into channel bfunc f2(a <-chan int, b chan<- int) { for { x, ok := <-a if !ok { break } (x) b <- x * 10 } }
3. Channel status
channel | nil not initialized | Empty channel | Full channel | Not empty |
---|---|---|---|---|
take over | block | block | Received value | Received value |
send | block | Send value | block | Send value |
closure | panic | Close successfully | Close successfully | Close successfully |
Data returned after closing | panic | Return 0 value | Returns zero value after reading the data | Return zero value after reading the data |
IV. Analysis of the causes of channel deadlock
Pay attention to the following situations:
When using a channel, we can see from the above table that sometimes it will enter a blocking state. Combined with waitGroup, if the main function waits for the function using the channel to end, and the function using the channel and the channel falls into a blocking state, if there are other functions to wake it up, it will not be deadlocked. If no other function can wake it up, a deadlock exception will be thrown.
Summarize:
The channel isolates the data in each channel, and can use the data well in concurrency. Of course, you must be familiar with several situations of channel blocking to avoid deadlock exceptions.
This is the end of this article about channel channel details in Go. For more relevant channel channel content in Go, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!