This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

Let’s move on to another important aspect of Golang: channels. A channel can be thought of as a conduit for Goroutines communication. Similar to the flow of water in a pipe from one end to the other, data can be sent from one end to the other and received through a channel.

When we talked about concurrency in Go, we said that the Go language strongly recommends the use of a Channel for communication between Goroutines, although it provides a traditional synchronization mechanism when multiple Goroutines want to share data.

“Don’t communicate by sharing memory, communicate by sharing memory” is a classic phrase that has swept the Golang community

In Go, to pass some data to another Goroutine (coroutine), you can encapsulate the data into an object and pass a pointer to the object into a channel from which another Goroutine reads the pointer and processes the memory object to which it points. Go provides developers with an elegant and simple tool to ensure that only one Goroutine can access the data in a channel at a time. So Go uses channels to communicate, passing memory data through communication, and passing memory data between different Goroutines. Instead of using shared memory to communicate.

Channel concept

What is a channel? A channel is a channel between goroutines. It allows goroutines to communicate with each other.

Each channel has a type associated with it. This type is the type of data that the channel allows to be transferred. The zero value of the channel is nil. Nil channels are useless, so channels must be defined using methods similar to map and slicing.)

Declaration of channels

The syntax for declaring a channel is the same as for defining a variable:

// Declare the channel
varChannel namechanThe data type// Create channel: If the channel is nil, you need to create the channel firstChannel name =make(chanData type)Copy the code

Sample code:

package main

import "fmt"

func main(a) {
	var a chan int
	if a == nil {
		fmt.Println("Channel is nil, you can't use it, you have to create the channel.")
		a = make(chan int)
		fmt.Printf("Data type: %T", a)
	}
}
Copy the code

Running results:

The channel isnil, cannot be used, need to create a channel first. The data type is:chan int
Copy the code

You can also make a brief statement:

a := make(chan int) 
Copy the code
The data type of channel

A channel is a reference type of data that, when passed as a parameter, passes a memory address.

Sample code:

package main

import (
	"fmt"
)

func main(a) {
	ch1 := make(chan int)
	fmt.Printf("%T,%p\n",ch1,ch1)

	test1(ch1)

}

func test1(ch chan int){
	fmt.Printf("%T,%p\n",ch,ch)
}
Copy the code

Running results:

We can see that ch and CH1 have the same address, which means they’re on the same channel.

Attention points of the channel

Note the following when using a Channel:

  • 1. Used for goroutine, passing messages.
  • 2. Channels, each with an associated data type, nil chan, cannot be used, similar to nil map, cannot store key/value pairs directly
  • < -chan < -data, send data to the channel. Write data to the channel data < -chan, get data from the channel. Read data from a channel
  • 4. Block: send data: chan < -data, blocked, until another goroutine reads data to unblock: data < -chan, also blocked. Until another goroutine writes out data and unblocks.
  • 5. The channel itself is synchronous, which means that only one Goroutine can operate at a time.

Finally: Channels are connections between goroutines, so channels must be sent and received in different Goroutines.