Go Learning notes-co-process

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Error:

Fatal Error:all Goroutines is asleep-deadlock!

Goroutine 1 [Chan receive]:

1, channel Buffer default is 0, directly write join error, errors as above

c: = make (chan int) without buffer channel
C <-1

Writes to a channel without a buffer, waits for the operation to be received, and ensures that the value is successfully received.
Will cause infinite waiting

With paired goroutine read, normal execution

c: = make (chan int)
Go func () {
C <-1
C <-2
C <-3

}()
<-c

Blocking order without buffers:

c: = make (chan int)
Go func () {
C <-1
Fmt. PRINTLN (4)
C <-2
Fmt. PRINTLN (5)
C <-3
Fmt. PRINTLN (6)
}()
Fmt. PRINTLN (7)
Fmt. Println (<-C)
Fmt. PRINTLN (8)
Fmt. Println (<-C)
Fmt. PRINTLN (9)
Fmt. Println (<-C)
Fmt. PRINTLN (10)

Output:

7

4

1

8

2

9

5

6

3

10

3, for range will always read the value within the channel, if no close will be error, error as above

c: = make (chan int, 1)

Go func () {

C <-1

C <-2

C <-3

Close (c)

}()

For I: = Range C {

Fmt. Println (i)

}

4, Select Case will be executed as soon as possible to return the case, can not return as soon as possible, will block waiting
The case expression will be executed



  

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.