Get the App
SLTechnology News&Howtos  ›  Development  › 

Go language basic concurrent channel

Shulou Source: shulou.com Published: 2022-06-03 19:23:47 11月12日 Update

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

Tags: Express pipeline Zhang San channel code Li Si type that is data language way thread pipe buffer foundation language foundation thing size example result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Huawei NVidia MariaDB