Use of the channel of Go

Source: Internet
Author: User

Func Count (ch Chan int) {counter++ch <-counterfmt. Println ("Counting")}func main () {CHS: = make ([]chan int, ten) for I: = 0; i <; i++ {chs[i] = make (chan int) go Count (ch S[i])}for index, ch: = Range CHS {FMT. PRINTLN (Index, "", <-ch)}} Output consistent result: COUNTING0 11 102 103 104 105 106 107 108 109 10

The guess is that when the range method reads the channel (CHS), it reads the first one, then reads all the rest, and if the remaining pipe buffer data does not fill n, it blocks

Another option:

Func Count (ch chan int) {ch <-countercounter++fmt. Println ("Counting")} Output consistent result: COUNTING0 01 12 13 14 15 16 17 18 19 1


Use of the channel of Go

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.