Go:channel (not finished)

Source: Internet
Author: User

Note : 1) All discussions below are based on the channel type that contains the shaping element, the Chan int

2) My understanding of "<-" is that it may be an operator (receive operator), or

May be part of a type (for example, "chan<-int" means the type of Send channel containing the shaping element)

Channel with Buffering and without cache

1. With cushioning: Ch:=make (Chan int,1)

1) Goruntine A contains the statement ch<-1: To send 1 to CH, if there is already a data in CH, then a is blocked here until the data in CH is taken away;

2) Goruntine A contains the statement <-ch: To receive a data from CH, if there is no data in CH, then a is blocked here until there is data in ch;  

3) Assume Ch:=make (Chan int,100), fori: = Range ch {...}. It is important to note that when traversing CH through range:

A. Range loop receives CH, until close (CH), if there is no data, will block here. 
    B. When close (CH) is run, it is not possible to send data to CH, but can still receive CH remaining data until the data in ch is empty, and the range statement ends instead of blocking.
  

2. Without buffering: Ch:=make (chan int), at this time the CH only acts as data transfer , cannot store the data (because it does not have buffer).

1) Goruntine A contains ch<-1: Run to here a block immediately unless another goruntine B is executing ch<-

In other words: A want to send data to CH, only when B is ready to receive data from CH;

2) in the same vein, Goruntine A contains <-ch. A to receive a data from CH, only when B is ready to send data to Ch.

  

type conversion : Be sure to note that Chan int is a whole

<-chan Int (v): Converts v to a channel type and then receives a value from that channel ("<-" is an operator at this point)

(<-chan int) (v): Convert V to a receive Channel type (at which point "<-" is part of the type)

Go:channel (not finished)

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.