Example of channel code in Golang----unbuffered, buffered, range, close

Source: Internet
Author: User

// code_043_channel_unbuffered project main.gopackage mainimport ( "fmt" "time")func main() { c := make(chan int, 0) //无缓冲的通道 //内置函数 len 返回未被读取的缓冲元素数量, cap 返回缓冲区大小 fmt.Printf("len(c)=%d, cap(c)=%d\n", len(c), cap(c)) go func() { defer fmt.Println("子协程结束") for i := 0; i < 3; i++ { fmt.Printf("子协程正在运行[%d]: len(c)=%d, cap(c)=%d\n", i, len(c), cap(c)) c <- i //备注:如果在上面的话, 不会执行最后一次Printf } }() time.Sleep(2 * time.Second) //延时2s for i := 0; i < 3; i++ { num := <-c //从c中接收数据,并赋值给num fmt.Println("num = ", num) } fmt.Println("main协程结束")}
 //code_044_channel_buffered project Main.gopackage mainimport ("FMT" "Time")//Buffered channel (buffered channel) is A channel that can store one or more values before being received. The receive action is blocked only if there are no values to receive in the channel. The send action is blocked only if the channel does not have an available buffer to hold the value being sent.    The difference between a buffered channel and a unbuffered channel://1) The unbuffered channel guarantees that the goroutine that is sent and received will be exchanged at the same time;//2) has a buffered channel without this guarantee Func main () {c: = make (chan int, 3) Fmt. Printf ("Len (c) =%d, Cap (c) =%d\n", Len (c), Cap (c)) go func () {defer FMT. Println ("Sub-process End") for I: = 0; I < 3; i++ {c <-i fmt. Printf ("Sub-process is running [%d]: Len (c) =%d, Cap (c) =%d\n", I, Len (c), Cap (c))}} () time. Sleep (2 * time. Second) for I: = 0; I < 3; i++ {num: = <-c fmt. Println ("num=", num)} FMT. Println ("Main coprocessor End")}  
 //Code_045_channel_range_close project Main.gopackage mainimport ("FMT")//Note://?channel does not need to be shut down as often as a file. Only if you really don't have any data to send, or do you want to explicitly end the range loop or something like this before you close channel;//? After the channel is closed, data cannot be sent to the channel (resulting in a panic error that causes the receive to immediately return a value of 0);/? After the channel is closed, the data can continue to be received from the channel;//for nil channel, regardless of the transmission will be blocked.        /* Close () use Func main () {c: = make (chan int) go func () {for i: = 0; i < 5; i++ {C <-i }//Leave Close (c) commented out and the program will always block if data, OK: = <-c;        OK that line//comment after the error "deadlock": Fatal Error:all Goroutines is asleep-deadlock! Close (c)} () for {//ok is true to indicate that the channel is not closed, false to indicate that the pipeline has closed if data, OK: = <-c; OK {fmt. PRINTLN (data)} else {break}} FMT. Println ("finished")}*/func main () {c: = make (chan int) go func () {for i: = 0; i < 5; i++ {C <-i} close (c)} () for data: = Range C {fmt. PRINTLN (data)} FMT. Println ("Finished")}  

The channel code example in

Golang----unbuffered, buffered, range, close

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.