Go language basic concurrent channel
This article mainly explains "Go language basic concurrent channel". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Go language basic concurrent channel".
Why do you need channel
Channel in Go, also known as pipes, is used to share data among multiple threads.
In general, channel is also used to share data in Go, but there are two ways to share data in Go.
Shared memory for communication.
Communicate via channel (recommended).
Why is shared memory communication not recommended?
Sample code: multithreading modifies a value.
Function
Func Calc () {defer wg.Done () NUM = NUM-1}
Main
Var NUM = 100var wg sync.WaitGroup func main () {for I: = 0; I