The last article looked at goruntine, and this one looks at communication between coroutines. It’s easy to understand. It’s called a channel, and its purpose is to safely transfer values between goruntine and Goruntine. You can create a channel using the make keyword:
test:=make(chan int)
Copy the code
The test channel has only two methods, using < -accept content and < -send content. <- on the left side of the channel for accept content and <- on the right side of the channel for send content.
package main
import "fmt"
func main(a) {
ch := make(chan string)
go func(a) {
ch <- "I'm the value sent by the coroutine."
}()
fmt.Println("I'm the main thread")
data := <-ch
fmt.Println(data)
fmt.Println("Program completed")}Copy the code
Channel is a presence of buffer in this example, sending and receiving is happening at the same time, no capacity to store any data in the channel, no regulation capacity, not like in the last example write coroutines waiting in the main thread 1 second let coroutines task execution output, in this case if you don’t send content to the channel coroutines task, The code that receives the content of the main program will always wait for the channel to send data, and will not run the code below the main program until it receives the content.
Buffer channel
Related to the unbuffered channel is the buffered channel. Defining the buffered channel requires specifying the capacity of the channel. The internal elements are first in first out, which reminds me of the stack, where the heap is first in first out and the stack is first in last out. A buffered channel can also have two states of task blocking. One is when there is no accepted value in the channel and the receiving operation is blocked, and the other is when the channel is full and the sending operation is blocked. A classic usage scenario would use a for loop to read or accept channel contents until the channel is closed:
for x := range ch{
fmt.Println(x)
}
Copy the code
This will stop the loop as long as the channel is closed and no errors will be reported. Write a simple function to do this:
package main
import (
"fmt"
"strconv"
)
func main(a) {
ch := make(chan string)
go func(a) {
for i := 0; i <= 10; i++ {
ch <- "Send" + strconv.Itoa(i)
}
close(ch)
}()
fmt.Println("I'm the main thread")
for x := range ch {
fmt.Println("Accept:" + x)
}
fmt.Println("Program completed")}Copy the code
Here to supplement the knowledge of closing channels, useclose()
Method to close the channel. Simple straightforward, follow the usego
It’s as simple as starting a coroutine,
conclusion
Channel in the language still has a lot of usage, is a beginner, you can understand it now it is doing just that, the language recommended not to communication through the Shared memory, and should Shared memory “through communication, multithreading want to read to modify resources in traditional language through the study of the locking and unlocking of the memory for communication to ensure the safety of data, In the Go language channel, it is equivalent to using the channel to open up a shared memory to declare the control of a resource, and using the channel to carry out messaging, which is more coherent and reliable.