Non-Blocking Channel Operations {.en}

非阻塞通道操作 {.zh}

::: {.en} Basic sends and receives on channels are blocking. However, we can use select with a default clause to implement non-blocking sends, receives, and even non-blocking multi-way selects. :::

::: {.zh}

常规的通过通道发送和接收数据是阻塞的。然而,我们可以 使用带一个 default 子句的 select 来实现非阻塞 的 发送、接收,甚至是非阻塞的多路 select。

:::

  1. package main
  2. import "fmt"
  3. func main() {
  4. messages := make(chan string)
  5. signals := make(chan bool)

::: {.en} Here’s a non-blocking receive. If a value is available on messages then select will take the <-messages case with that value. If not it will immediately take the default case. :::

::: {.zh}

这里是一个非阻塞接收的例子。如果在 messages 中 存在,然后 select 将这个值带入 <-messages case 中。如果不是,就直接到 default 分支中。

:::

  1. select {
  2. case msg := <-messages:
  3. fmt.Println("received message", msg)
  4. default:
  5. fmt.Println("no message received")
  6. }

::: {.en} A non-blocking send works similarly. Here msg cannot be sent to the messages channel, because the channel has no buffer and there is no receiver. Therefore the default case is selected. :::

::: {.zh}

非阻塞发送的工作方式类似。这里 msg 不能发送到 messages 通道,因为通道没有缓冲区而且没有接收器。因此选择了 default 分支。

:::

  1. msg := "hi"
  2. select {
  3. case messages <- msg:
  4. fmt.Println("sent message", msg)
  5. default:
  6. fmt.Println("no message sent")
  7. }

::: {.en} We can use multiple cases above the default clause to implement a multi-way non-blocking select. Here we attempt non-blocking receives on both messages and signals. :::

::: {.zh}

我们可以使用 default 子句 上面的多个 case 来实现多路非阻塞选择。在这里,我们尝试非阻塞的接收 messagessignals

:::

  1. select {
  2. case msg := <-messages:
  3. fmt.Println("received message", msg)
  4. case sig := <-signals:
  5. fmt.Println("received signal", sig)
  6. default:
  7. fmt.Println("no activity")
  8. }
  9. }
  1. $ go run non-blocking-channel-operations.go
  2. no message received
  3. no message sent
  4. no activity