golang channel usage

王林
Release: 2023-05-16 10:25:37
Original
652 people have browsed it

Golang is a popular programming language because of its efficiency, memory safety, and concurrency. In Golang, concurrent communication can be easily achieved using channels, which is also one of the major advantages of Golang. This article will provide an in-depth introduction to the basic concepts, usage and some techniques of channels in Golang.

  1. What is channel

In Golang, channel is a data structure used for Goroutine communication. It can be buffered or unbuffered. An unbuffered channel is a synchronous channel that blocks both sending and receiving data. When the sent data is not received, the send operation will be blocked until a Goroutine receives the data. Similarly, when the receiver is not ready to receive data, the receive operation will be blocked. A buffered channel allows data to be sent and received before the buffer is full, and the buffer capacity is the size specified when the channel is created.

Channel can be defined and initialized in the following way:

var ch chan int // 定义一个 int 类型的 channel

ch = make(chan int) // 初始化 channel

ch := make(chan int) // 定义并初始化 channel
Copy after login
  1. Usage of channel

In Golang, when using channel, it usually involves the following Two operations:

  • Send data
  • Receive data

Basic syntax for sending data through channel:

ch <- data  // 将 data 发送到 channel ch 上
Copy after login

Receive through channel Basic syntax of data:

data := <- ch // 从 channel ch 中接收数据,并将其赋值给变量 data
Copy after login

Note: The receiving operation will block until data is sent to the channel. If the channel has been closed and there is no data in it, the receiving operation will return immediately and assign a zero value to the receiving Variables.

The following is a simple example:

package main

import "fmt"

func main() {
    ch := make(chan int)
    go sendData(ch)

    for i := range ch {
        fmt.Println("Received:", i)
        if i == 10 {
            break
        }
    }
}

func sendData(ch chan int) {
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch) // 关闭 channel
}
Copy after login

In the above example, we define a sendDate function to send data to ch. In the main function, we create a channel and use the range method to receive data from the channel one by one. When the received data is 10, we break out of the loop and close the channel.

  1. The blocking characteristics of channel

In Golang, channel is a blocking data structure, which means that when Goroutine tries to send data to a full channel or They are blocked when receiving data from an empty channel. This helps ensure synchronization and collaboration between Goroutines.

Next, we will demonstrate the blocking characteristics of channel through an example:

package main

import "fmt"

func main() {
    ch := make(chan int, 2)

    ch <- 1 // 发送数据
    ch <- 2

    fmt.Println(<-ch) // 接收数据
    fmt.Println(<-ch)

    ch <- 3
    fmt.Println("数据已经被发送,但是没有接收方")
}
Copy after login

In this example, we create a channel with a buffer size of 2 and try to send it 3 data. The first two data can be sent because the buffer is not full yet. But when we try to receive this data, we need to wait for the receiver of each data one by one before we can continue sending. On the third attempt to send data to the channel, the sender will be blocked because there is no receiver.

  1. Closing of channel

In Golang, channel can be closed through the close method. A closed channel will return a zero value when received and will not be blocked. This allows the receiver to accurately determine whether the channel has been closed after receiving the data.

The following is an example:

package main

import "fmt"

func main() {
    ch := make(chan int)

    go sendData(ch)

    for i := range ch {
        fmt.Println("Received:", i)
    }
}

func sendData(ch chan int) {
    for i := 0; i < 10; i++ {
        ch <- i
    }
    close(ch) // 关闭 channel
}
Copy after login

In this example, when the sendDate function finishes sending the data, it will use the close method to close the channel. In the main function, we use the range method to receive the data transmitted from the channel; when there is no data in the channel, the range method will exit the loop.

  1. Channel selector

In addition to the above basic channel operations, Golang also provides a selector (select) that can be used between multiple channels choose. This is useful for handling send and receive operations on multiple channels.

The following is an example:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan int)
    ch2 := make(chan int)

    go func() {
        time.Sleep(2 * time.Second)
        ch1 <- 1
    }()

    go func() {
        time.Sleep(5 * time.Second)
        ch2 <- 2
    }()

    for i := 0; i < 2; i++ {
        select {
        case <-ch1:
            fmt.Println("Received from ch1")
        case <-ch2:
            fmt.Println("Received from ch2")
        }
    }
}
Copy after login

In this example, we define two channels. Send data to the channel after a delay of 2 seconds and 5 seconds in the two Goroutines respectively. Then, in the main function, we use the select method to listen to both channels. When there is data in the channel, the select method will respond.

  1. Summary

In Golang, channel is a very powerful and easy-to-use tool. Through channels, we can achieve communication and synchronization between Goroutines, thereby improving program concurrency performance. This article introduces the basic concepts, usage and some techniques of channels, hoping to help readers better understand how to use channels to build reliable concurrent programs.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template