Home  >  Article  >  Backend Development  >  golang chan usage

golang chan usage

PHPz
PHPzOriginal
2023-05-16 15:12:482977browse

Golang is a strongly typed language known for its high concurrency and concise syntax. Among them, chan is one of the common communication methods in Golang and an important part of implementing concurrent programming. In this article, we will take an in-depth look at the usage and fundamentals of chan in Golang.

1. The concept and function of chan

Chan is an important way to achieve communication between goroutines in Golang, referred to as pipeline. It is a thread-safe data structure used to pass information in Golang programs. chan can implement one-way communication and two-way communication, can be used to send and receive data, and can also be used to synchronize goroutines.

2. Types and usage of chan

Chan in Golang is a type that can be created using the make function. The syntax is as follows:

ch := make(chan int)

where int represents the type of data passed in the pipeline. When using chan, you need to pay attention to the following points:

  1. chan is blocking

Both sending and receiving operations are blocking, that is, if there is no matching of sending and receiving operations , then the goroutine will always be blocked on this operation. For example:

ch := make(chan int)

// 发送操作
go func() {
   ch <- 1
}()

// 接收操作
a := <- ch

In this example, we created a pipe of type int and performed sending and receiving operations respectively. In the send operation, we send a value 1 to the pipe; in the receive operation, we take the value out of the pipe and assign it to the variable a. Since both send and receive operations are blocking, this program will wait until the send and receive operations match before it can end normally.

  1. Close chan

You can use the close function to close the pipe. The closed pipe cannot be sent again. For example:

ch := make(chan int)

// 发送操作
go func() {
   ch <- 1
   close(ch)
}()

// 循环接收操作
for {
   if val, ok := <-ch; ok {
      fmt.Println(val)
   } else {
      break
   }
}

In this example, we call the close function after the send operation, and then use a for loop to receive the pipeline. In the receiving operation, ok is used to determine whether the pipeline has been closed to prevent deadlock.

  1. One-way chan

One-way chan can be created by setting the direction of the pipe. For example:

ch := make(chan int) // 双向chan

// 定义只能发送的单向chan
sendCh := make(chan <- int)

// 定义只能接收的单向chan
recvCh := make(<- chan int)

// 发送操作时可以使用单向chan
go func() {
   sendCh <- 1
}()

// 接收操作时也可以使用单向chan
a := <-recvCh

In this example, we create a two-way chan through the make function, and then create one-way chan that can only send and only receive through the make function. In the sending operation and receiving operation, we use sendCh and recvCh respectively.

  1. select statement

The select statement can monitor the status of multiple pipelines at the same time and can be used for concurrent read and write operations of pipelines. For example:

ch1 := make(chan int)
ch2 := make(chan int)

// 发送操作
go func() {
   ch1 <- 1
}()

// 使用select语句并发监听多个管道
select {
case a := <- ch1:
   fmt.Println(a)
case b := <- ch2:
   fmt.Println(b)
}

In this example, we created two pipelines ch1 and ch2 and sent the value 1 to ch1 in a goroutine. After that, we used the select statement to listen to the two pipes, and the case statement that received the first value was executed first.

3. The basic principle of chan

In Golang, chan is implemented based on a special data structure. When we use the make function to create chan, we actually create a slice with a value of nil and a length of 0, called a channel.

We can understand the principle of chan in the following way:

  1. Send operation

When performing a send operation, the data to be sent will be appended to the slice at the bottom of the channel. If the length of the channel is 0, then the index of the added element is 0. If the length of the channel is not 0, the index of the added element is the length of the channel.

If the length of the channel has reached its upper capacity limit, a larger slice will be created in the memory and the elements in the original slice will be copied to the new slice. Therefore, when performing a send operation, memory management and copying mechanisms will be used.

  1. Receiving operation

When performing a receiving operation, the first element appended will be taken out from the slice at the bottom of the channel. If there are no elements in the slice, it will wait until an element is available; if the channel has been closed, the receiving operation will immediately return a zero value.

  1. Blocking

When performing a send or receive operation, if the channel's slice length has reached its capacity limit, or there is data waiting to be received in the channel, then send Or the receive operation will block until sufficient space or data is available.

  1. Close operation

When closing a channel, the status of the channel will be set to closed and no more data can be sent. If there is unreceived data in the channel, the receiving operation can continue until there is no data in the channel.

Summary

Chan in Golang is an important way to achieve communication between goroutines, and it is also very concise in terms of syntax. Mastering the basic usage and principles of chan is very important for concurrent programming.

The above is the detailed content of golang chan usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn