为什么同一个 Go Channel 不能启动两次?

Why same Go Channel can't be started twice?

我们可以在 Go lang 中启动一个 decalred 通道两次吗?

package main

import (
    "fmt"
)

func emit(c chan string) {

    words := []string {"The", "quick", "brown", "fox"}

    for _, word := range  words {
        c <- word
    }
    close(c)
}

在函数 main 中,如果我尝试使用同一个频道两次,我将获得该频道的默认值

func main() {

    wordChannel := make(chan string)

    go emit(wordChannel)

    for word := range wordChannel {
        fmt.Printf("%s ", word)
    }

    go emit(wordChannel)
    word1 := <-wordChannel
    fmt.Printf("%s" , word1) // prints Default value
}

Output

所以要再次使用它,我必须声明另一个频道。 如果这不是错误,为什么要在 Go Lang 中完成。 ? 我正在使用 go -lang 版本 1.6

频道不是 "started",频道只是存在并且处于以下两种状态之一:

  • "open" 在这种情况下,您可以向他们发送值(并接收发送的值),或者
  • "closed" 您无法从关闭的频道发送和接收结果 "the-zero-value, false"。

一旦关闭的频道将永远关闭。所以是的,你必须 make 一个新频道,Go 中没有 "reopen"。