This is a creation in Article, where the information may have evolved or changed.
package mainimport ( "fmt")func main() { make(chanint) /* 下面2行不能互换,主程序顺序执行,若先遇到 ch<-2,则在此阻塞了。程序不会向下执行 */ go//开启一个协程 ch <- 2 //向channel中传送数据}funcchanint) { //输出数据}
If the capacity is greater than 0, the channel is asynchronous: The buffers are not blocked until the full load (send) or empty (receive), the elements are received in the order in which they are sent. If the capacity is 0 or not set, the communication can only succeed if both the sending and receiving parties are ready.
To design an algorithm in the first place using a unbuffered channel, use buffering only in the case of uncertainty.
PackageMainImport("FMT")funcMain () {ConstNint=TenData: = []int{, at, $, at,5,2,1,456, the, $}//10 elementsCH: = Make(Chan int) forIndex, V: =RangeData {Go func(Index, Vint) {ch <-v fmt. Printf ("Index is%d,value is%d \ n", index, V)} (index, v)} forI: =0; i < N; i++ {fmt. Println (<-ch)}}
The output result is
Index is 0,value is 34
34
23
45
23
5
2
1
55W
76
46
Explain:
When the program executes linearly to the first for loop, the Goroutine within the for loop begins executing concurrently (that is, restarting the coprocessor execution), and after 10 cycles, 10 concurrent threads (more lightweight than thread) are turned on, and the main program is equivalent to running 10 empty loops.
On the one hand, the main thread executes the next for loop, on the one hand the 10 concurrent threads execute concurrently.
Main thread when encountering the first fmt.Println(<-ch)
, see if there is a value in the channel, if the value is read and output, no value is waiting for blocking.
The goroutine performs inline functions, ch <- v
passing values to the channel and blocking if the wireless path is accepted. Because the second for of the main thread has a receive value, the program exits when the main thread receives the value. The output statement in the resultant visible path only prints one (0-n, random), because the main program exits when the N channel value is received, and the print statement is passed in the process.
Then we look at the next example, just specifying the size when the channel is created, and the channel is a cached channel.
PackageMainImport("FMT")funcMain () {ConstNint=TenData: = []int{, at, $, at,5,2,1,456, the, $} ch: = Make(Chan intN forIndex, V: =RangeData {Go func(Index, Vint) {ch <-v fmt. Printf ("Index is%d,value is%d \ n", index, V)} (index, v)} forI: =0; i < N; i++ {fmt. Println (<-ch)}}
Output Result:
Index is 0,value is 34
Index is 1,value is 23
Index is 2,value is 45
Index is 3,value is 23
Index is 4,value is 5
Index is 5,value is 2
Index is 6,value is 1
Index is 7,value is 456
Index is 8,value is 76
Index is 9,value is 46
34
23
45
23
5
2
1
456
76
46
The
Only changes the channel into a cached result, because there are 10 threads written, and only one is read (the main thread). The speed of writing is faster than reading. When there is no cache, the situation is to read a write one. When there is a cache, you do not have to wait for reading, you can write the cache directly,