You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go语言select语句示例解析请求:新手学习疑问

Hey there! Let's break down this Go select statement example step by step to help you understand how it works. First, let's fix the incomplete code snippet you provided to make it runnable (the default case was cut off):

package main
import "fmt"
func main() {
    var c1, c2, c3 chan int
    var i1, i2 int
    select {
        case i1 = <-c1:
            fmt.Printf("received %d from c1\n", i1)
        case c2 <- i2:
            fmt.Printf("sent %d to c2\n", i2)
        case i3, ok := (<-c3): // same as: i3, ok := <-c3
            if ok {
                fmt.Printf("received %d from c3\n", i3)
            } else {
                fmt.Printf("c3 is closed\n")
            }
        default:
            fmt.Printf("No channel operations are ready to execute!\n")
    }
}

Key Background on select

First, a quick recap: The select statement in Go lets you wait on multiple channel operations at once. It will block until one of the case clauses is ready to execute. If multiple cases are ready, it picks one randomly. If none are ready and there's a default clause, it runs that immediately.

Line-by-Line Breakdown of the Example

Let's go through each part of the select block:

  1. First case: case i1 = <-c1:

    • This is a channel receive operation: it tries to pull a value from c1 and assign it to i1.
    • But here's the catch: c1 is declared with var c1 chan int but never initialized (so it's nil). A nil channel's receive operation will block forever—this case will never be triggered.
  2. Second case: case c2 <- i2:

    • This is a channel send operation: it tries to send the value of i2 (which is 0, since it's an uninitialized int) to c2.
    • Again, c2 is nil. Sending to a nil channel also blocks forever, so this case won't run either.
  3. Third case: case i3, ok := (<-c3):

    • This is a receive operation with an ok flag, which lets us check if the channel is closed.
    • Once more, c3 is nil. Receiving from a nil channel blocks forever, so this case can't execute. Even if c3 were closed later, since it's nil right now, this code path is unreachable in this example.
  4. Default case

    • Since all three channel operations are blocked (thanks to the nil channels), the select falls back to the default clause immediately. It will print No channel operations are ready to execute! when you run the code.

What This Example Teaches You

This is a great demonstration of what happens when you use select with uninitialized (nil) channels: all send/receive operations on nil channels block indefinitely, so if you have a default clause, that's the only code that will run.

A few extra notes to remember:

  • If there was no default clause here, the select would block forever, because none of the channel operations can complete.
  • When multiple non-blocking cases are ready, select picks one at random to prevent starvation (no single case gets prioritized over others).
  • The ok flag in receive operations is useful: when a channel is closed and has no more values to read, ok will be false, letting you handle closed channels gracefully.

内容的提问来源于stack exchange,提问作者Andy Hermann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:41:34