In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the role of Channel data structure in Golang, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Requirements description of channel
To understand what problems these data structures solve, let's do a brief review to see why these two data structures are needed and what problems they solve. We know that goroutine is a user-mode thread, and there is a need for messaging between different goroutine. In the original process and thread (system thread) programming, we will use the pipeline approach, and channel is the pipeline implementation of user-mode threads to deliver messages, and is type-safe.
Since channel is a pipeline, it is used to exchange messages between different goroutine. So how to implement such a pipeline?
Let's take a look at our daily message delivery needs:
Can have multiple goroutine to read and write to channel, and ensure that there is no competition problem, need to use queue to manage blocked goroutine, solve competition problem; need to manage messages sent to channel (buffered, unbuffered), for cached channel can use circular queue to manage multiple messages.
Of course, the above requirements are simplified, for example, channel also needs to have the ability to block and wake up goroutine, but for this article, let's focus more on the above two issues.
Data structure of channel
Next, let's analyze what the structure of channel looks like in practice. Of course, this can be divided into two types, buffered and unbuffered. Let's start with an unbuffered situation.
No buffer
Post the sample code first. That is, two read goroutine are blocked on an unbuffered channel.
Func main () {
Ch: = make (chan int) / / No buffering
Go goRoutineA (ch)
Go goRoutineB (ch)
Ch
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.