首页  >  文章  >  后端开发  >  为什么Go中需要等待select?

为什么Go中需要等待select?

王林
王林转载
2024-02-11 08:24:091159浏览

为什么Go中需要等待select?

php小编子墨介绍:在Go语言中,select语句是一种非常重要的控制流程语句,它用于同时监听多个通道的操作,实现并发控制。为什么需要等待select呢?这是因为在并发编程中,我们通常需要同时处理多个通道的数据或事件,而select语句可以帮助我们监听多个通道,一旦其中任意一个通道可操作,就会执行对应的操作,从而实现并发处理。通过使用select,我们可以有效地避免阻塞,提高程序的响应性和并发能力。

问题内容

我刚刚学习了上下文取消。 这是我的代码。

package main

import (
  "fmt"
  "context"
)

func main() {
  ctx := context.Background()

  do(ctx)
}

func do(ctx context.Context) {
  ctx, ctxCancel := context.WithCancel(ctx)

  resultCh := make(chan string)

  go terminate(ctx, resultCh)

  resultCh <- "value1"
  resultCh <- "value2"

  fmt.Println("pre cancel")

  ctxCancel()

  fmt.Println("post cancel")
}

func terminate(ctx context.Context, ch <-chan string) {
  for {
    select {
    case <-ctx.Done():
      fmt.Println("terminate")
      return
    case result := <-ch:
      fmt.Println(result)
    }
  }
}

想问

为什么会发生这种情况。 我需要什么知识?

我期待输出。

但得到的实际输出不包含“终止”。

value1
value2
pre cancel
terminate
post cancel

固定代码

我在取消功能下添加了 time.Sleep 。 那么输出就是我的预期。

ctxCancel()
time.Sleep(100 * time.Millisecond) // add

解决方法

据我了解,使用 select 背后的核心思想是等待至少一种情况“准备好”。我在下面提供了一个示例,可能会有所帮助。这里 select 用于等待从通道 ch 接收值或发生 1 秒超时。

import (
    "fmt"
    "time"
)

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

    go func() {
        // Simulate some time-consuming operation
        time.Sleep(2 * time.Second)
        ch <- 42
    }()

    select {
    case result := <-ch:
        fmt.Println("Received result:", result)
    case <-time.After(1 * time.Second):
        fmt.Println("Timeout reached")
    }
}

以上是为什么Go中需要等待select?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除