gopl-zh.github.com/ch8/ch8-04.md

49 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 8.4. Channels
如果说goroutine是Go语言程序的并发体的话那么channels则是它们之间的通信机制。一个channel是一个通信机制它可以让一个goroutine通过它给另一个goroutine发送值信息。每个channel都有一个特殊的类型也就是channels可发送数据的类型。一个可以发送int类型数据的channel一般写为chan int。
使用内置的make函数我们可以创建一个channel
```Go
ch := make(chan int) // ch has type 'chan int'
```
和map类似channel也对应一个make创建的底层数据结构的引用。当我们复制一个channel或用于函数参数传递时我们只是拷贝了一个channel引用因此调用者和被调用者将引用同一个channel对象。和其它的引用类型一样channel的零值也是nil。
两个相同类型的channel可以使用==运算符比较。如果两个channel引用的是相同的对象那么比较的结果为真。一个channel也可以和nil进行比较。
一个channel有发送和接受两个主要操作都是通信行为。一个发送语句将一个值从一个goroutine通过channel发送到另一个执行接收操作的goroutine。发送和接收两个操作都使用`<-`运算符。在发送语句中,`<-`运算符分割channel和要发送的值。在接收语句中,`<-`运算符写在channel对象之前。一个不使用接收结果的接收操作也是合法的。
```Go
ch <- x // a send statement
x = <-ch // a receive expression in an assignment statement
<-ch // a receive statement; result is discarded
```
Channel还支持close操作,用于关闭channel,随后对基于该channel的任何发送操作都将导致panic异常。对一个已经被close过的channel进行接收操作依然可以接受到之前已经成功发送的数据;如果channel中已经没有数据的话将产生一个零值的数据。
使用内置的close函数就可以关闭一个channel
```Go
close(ch)
```
以最简单方式调用make函数创建的是一个无缓存的channel,但是我们也可以指定第二个整型参数,对应channel的容量。如果channel的容量大于零,那么该channel就是带缓存的channel
```Go
ch = make(chan int) // unbuffered channel
ch = make(chan int, 0) // unbuffered channel
ch = make(chan int, 3) // buffered channel with capacity 3
```
我们将先讨论无缓存的channel,然后在8.4.4节讨论带缓存的channel
{% include "./ch8-04-1.md" %}
{% include "./ch8-04-2.md" %}
{% include "./ch8-04-3.md" %}
{% include "./ch8-04-4.md" %}