# 10.4 重温Channel（通道）

一旦掌握了 `select` 关键字，Go channels 可以以几种独特的方式做更多事要比您在[第9章](https://github.com/hantmac/Mastering_Go_ZH_CN/tree/master/eBook/chapter9/09.0.md)（并发-Goroutines,Channel和Pipeline）学到的。这节将揭晓 Go channels 的这些使用方法。

要记住 channel 类型的零值是 `nil`，并且如果您发送一个消息给以关闭的 channel，程序将崩溃。然而，如果您尝试从已关闭的 channel 读取的话，会得到 channel 类型的零值。因此，关闭 channel 后，您不能再往里写，但您能一直读。

为了能关闭 channel, channel 不必是只接受。另外，一个 `nil` channel 总是阻塞的。channels 的这个特性非常有用，当您想要禁用 `select` 表达式的一个分支时，可以分配一个 `nil` 值给一个 channel 变量。

最后，如果您要关闭一个 `nil` channel，程序就会崩溃。最好的说明是下面这个 `closeNilChannel.go` 程序：

```go
package main

func main() {
    var c chan string
    close(c)
}
```

执行 `closeNilChannel.go` 产生如下输出：

```
$go run closeNilChannel.go
panic: close of nil channel

goroutine 1 [running]:
main.main()
    /Users/mtsouk/closeNilChannel.go:5 +0x2a
exit status 2
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/10.0/10.4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
