This is the 29th day of my participation in Gwen Challenge
These reviews
If you don’t already know Go, I suggest you read mineGo from zero.
In this series of articles, I’ll take a closer look at the Go language and introduce advanced features such as package management, reflection, and concurrency.
This article will continue the previous article, practice the application of channel.
Channel is the channel
Go advocates the use of channels as a means of synchronization and communication between Goroutines. A channel type is a reference type, and each channel can only pass a fixed type of data.
If we create a channel with a buffer, sending data to the channel when the buffer is full will not block the sending Goroutine, as shown in the following code example:
Consume (ch chan int) {// Consume (ch chan int) {// Consume (ch chan int) 100) < -ch} func main() {// make(chan int, Println("I am free!") consume(ch) ) ch <- 2 fmt.Println("I can not go there within 100s!" ) time.Sleep(time.Second) }Copy the code
Up to 100, the output will be:
I am free!
Copy the code
After the buffer is full, the Goroutine that continues to send data to the channel blocks until the data in the channel is read, i.e., when the cache is full.
In addition to declaring two-way channels, we can also declare one-way channels, that is, only send data from a channel or only read data from a channel, as shown in the following example:
Ch := make(chan T) var chInput chan < -int = ch var chOutput int < -chan = chCopy the code
A one-way channel is generally used to ensure the rigor of the code interface when transmitting the channel value, so that only those who need to read or send data can perform corresponding operations, avoiding the logic confusion caused by the transgressive misoperation.
If you need to accept messages from multiple channels in a Goroutine, you can use the multiplexing provided by the SELECT keyword in Go. The select keyword is used in much the same way as switch, except that the case statement must be followed by an I/O operation. Goroutine will block if there is no I/O response and no default statement is provided. A simple example is as follows:
Package main import (" FMT ""time") func send(ch chan int, begin int) {for I :=begin; i< begin + 10 ; i++{ ch <- i } } func main() { ch1 := make(chan int) ch2 := make(chan int) go send(ch1, 0) go send(ch2, Time.sleep (time.second) for {select {case val := < -ch1: Printf("get value %d from ch1\n", val) case val := < -ch2: Printf("get value %d from ch2\n", val) case <-time.After(2 * time.second): Println("Time out") return}}}Copy the code
Due to the uncertainty of Goroutine scheduling, a possible run result is:
get value 0 from ch1
get value 10 from ch2
get value 1 from ch1
get value 11 from ch2
get value 2 from ch1
get value 12 from ch2
get value 3 from ch1
get value 13 from ch2
get value 4 from ch1
get value 5 from ch1
get value 14 from ch2
get value 6 from ch1
get value 7 from ch1
get value 8 from ch1
get value 15 from ch2
get value 9 from ch1
get value 16 from ch2
get value 17 from ch2
get value 18 from ch2
get value 19 from ch2
Time out
Copy the code
In the above code, we read data from CH1 and CH2 using SELECT multiplexing. If ch arrives in multiple case statements at the same time, select will run a pseudo-random algorithm to randomly select a case. After(2 * time.second) I/O <-time.After(2 * time.second) I/O < time.after (2 * time.second) I/O = 2 seconds. The blocking of a channel cannot be broken, which is an effective trick to time out the return from a blocked channel.
summary
This paper mainly introduces the application practice of channel. As a new generation language, everything about Go is fresh and interesting, and it is constantly improving. The emergence of go Module effectively solves the problem of go dependency disorder. The ability to reflect also provides the developer with the possibility to make changes to the code at run, facilitating the implementation of various frameworks such as dependency injection frameworks; The unique MPG threading model and CSP concurrency concept also add to Go’s high performance and high concurrency features.
Read the latest article, pay attention to the public number: AOHO Qiusuo