Summary of channel usage in Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The channel in the Go language is the key mechanism to realize the goroutine communication between the two, which makes it easy, flexible and accessible to write multi-threaded concurrent programs. The following is a brief summary of what should be noted in the use of the channel as a personal understanding.
Channel classification: Without cache channel, with cache channel
(1) without cache channelSyntax: (a) Create channel make (chan type) e.g.CH: = make (chan int) (b) Communication mode (because Chan operates like a queue, enqueue,dequeue is used to describe communication operations here) Enqueue:ch <- TypevarDeQueue: var: = <-Ch e.g. CH <-1 V: = <-ch Critical: After the channel EnQueue operation is blocked (regardless of whether the channel is empty), until the written data is read out. When the channel DeQueue operation is invoked, if there is data in the channel, it is read out, and if it is empty, it is blocked until someone enqueue the data inside.
(2) with cache channelSyntax: (a) Create channel Make (Chan   type ,   size )e.g. CH: = Make (chan int, 9) (b) communication mode with no cache channel key: When the channel element is less than or equal to the channel size, the channel EnQ is called Ueue the data is placed into the cache (non-blocking), and when the channel is full, the enqueue operation is blocked until an element is DeQueueCome out. When the channel DeQueue operation is invoked, if the channel is empty, it blocks until someone enqueue the data inside, otherwise directly DeQueue out the element.
Note: It is important to note that the difference between the range operation is: No cache channel is enqueue a data is read by range, and the buffer channel is enqueue full after the range is taken out (this mechanism is transparent to the user, The user sees one or the other, or the timeout time will be taken out even if the channel is not full. In addition, after the channel is closed, the loop read channel operation (e.g. for V:=range Channel) reads the remaining data in the channel and automatically jumps out of the loop.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.