This is the 28th 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.

After understanding the goroutine practice in Go, this article will focus on the use and characteristics of channels.

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 transmit a fixed type of data. A channel declaration is as follows:

var channelName chan T
Copy the code

We declare a new channel with the chan keyword and specify the data type T transferred within the channel.

As a queue, a channel ensures the order of sending and receiving data and always follows the first-in, first-out principle. It also ensures that there is only one Goroutine access channel at a time for sending and retrieving data.

Sending data from a channel requires the <- symbol, as shown below:

channel <- val
Copy the code

This means that val will be sent to a channel, which will block the current Goroutine after the channel is filled. Reading data from a channel also uses the <- symbol, but the position of the data to be received and the channel are switched, as shown below:

val := <- channel
Copy the code

Read val from a channel. If there is no data in the channel, the read Goroutine will be blocked until data is put into the channel. It is also possible to return a channel immediately when it is read, as shown below:

val, ok:= <- channel
Copy the code

Check that OK is true to see if valid data has been read.

To create a channel we need to initialize it with the make function as follows:

ch := make(chan T, sizeOfChan)
Copy the code

When creating a channel, you need to specify the data type and optionally the length of the channel. If the length of a channel is not specified, the goroutine that sends data to the channel will block until the data is read. A channel of the specified length will carry a buffer of sizeOfChan, so sending data will not be blocked if the buffer is full. Whether or not a channel carries a buffer, the goroutine read is blocked until there is data in the channel to read.

We’ll create a channel to send data between two Goroutines. One of the Goroutines reads input from the command line and sends it to the channel. Another Goroutine loop reads data from a channel and prints it as follows:

Package main import ("bufio" "FMT" "OS") func printInput(ch chan string) {// Use for loop to read data from channel for val := If val == "EOF"{break} fmt.Printf("Input is %s\n", Val)}} func main() {// Create an unbuffed channel ch := make(chan string) go printInput(ch) // Read from the command line type scanner := Bufio.newscanner (os.stdin) for scanner.scan () {val := scanner.text () ch < -val if val == "EOF"{break}} // The program closes ch at the end defer close(ch) }Copy the code

Run the above code with Go Run and type a string on the command to get feedback from the program, as shown below:

Hello
Input is Hello
Hi
Input is Hi
EOF
End the game!
Copy the code

A channel, as a container with length, can also be traversed. In the code above, we loop through data from a channel using the for:range syntax. PrintInput’s Goroutine will block when there is no data in the channel. At the end of the code, we also closed the created channel by defer close(ch). It should be noted that no more data is allowed to be added to the channel after the channel is closed, otherwise panic will be thrown. A Goroutine that reads from a closed channel or is blocking will receive zero and return.

summary

This paper mainly introduces the application practice of channel. The Go language channel, as a container with length, can also be traversed. In the example above, we loop through data from a channel using the for:range syntax. PrintInput’s Goroutine will block when there is no data in the channel. At the end of the code, we also closed the created channel by defer close(ch). It should be noted that no more data is allowed to be added to the channel after the channel is closed, otherwise panic will be thrown. A Goroutine that reads from a closed channel or is blocking will receive zero and return.

Read the latest article, pay attention to the public number: AOHO Qiusuo