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