duanbeng1923 2016-08-18 10:16
浏览 19
已采纳

未初始化的通道表现如何?

I have a struct that holds a channel that is not initialized.

When I write into it, the routine is blocking as expected but the reader is never notified that something is in the pipe.

I am surprised that there is no error and I am wondering what is doing Go.

In the example bellow, neither the message pushed nor got it are printed. (Uncomment the intialization and it will work like a charm)

type MyStruct struct {
    over chan bool
}

func main() {
    nonInitialized := &MyStruct{}
    // nonInitialized.over = make(chan bool)
    go func() {
        for i := 0; i < 10; i++ {
            select {
            case <-nonInitialized.over:
                fmt.Println("got it")
            default:
                // proceed
            }
            fmt.Println("do some stuff")
            time.Sleep(10 * time.Millisecond)
        }
        panic("took too long")
    }()
    // push on the non initialized channel
    fmt.Println("pushing")
    nonInitialized.over <- true
    fmt.Println("pushed")
}

Here is the playground https://play.golang.org/p/76zrCuoeoh

(I know I should initialize the channel, this is not the purpose of the question, I want to know what is happening in Go with non initialized channels.)

  • 写回答

2条回答 默认 最新

  • dongyu1979 2016-08-18 10:36
    关注

    An "uninitialized" field or variable of channel type will have the zero value of all channel types which is nil. So let's examine how a nil channel or operations on it behave.

    It is worth collecting the channel axioms in one post:

    Reasoning for blocking in case of nil channels: if a channel value is nil, no one has a reference to it, so no one will ever be ready to receive from it (what we want to send); or send anything on it (what we would receive from it).

    You can read further reasoning and more details about this in Dave Cheney: Channel Axioms.

    For completeness:

    • Closing a nil channel causes a run-time panic (just like closing an already closed channel).
    • Length and capacity of a nil channel is 0; in accordance with nil slices and maps having 0 length and capacity.

    Reasoning: "closed" is a state, but a nil channel cannot have a state (there is only one nil channel, and not one for "closed" and one for "not closed" channel). And there are no elements queued in a nil channel (so len = 0), and it does not have a buffer capacity (so cap = 0).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思