mirror of
https://github.com/golang/go
synced 2024-11-23 04:40:09 -07:00
spec: several clarifications to language on channels
- A channel may be used between any number of goroutines, not just two. - Replace "passing a value" (which is not further defined) by "sending and receiving a value". - Made syntax production more symmetric. - Talk about unbuffered channels before buffered channels. - Clarify what the comma,ok receive values mean (issue 7785). Not a language change. Fixes #7785. LGTM=rsc, r, iant R=r, rsc, iant, ken CC=golang-codereviews https://golang.org/cl/94030045
This commit is contained in:
parent
dbe5f88804
commit
97aa90d251
@ -1278,20 +1278,23 @@ may be added.
|
|||||||
<h3 id="Channel_types">Channel types</h3>
|
<h3 id="Channel_types">Channel types</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A channel provides a mechanism for two concurrently executing functions
|
A channel provides a mechanism for
|
||||||
to synchronize execution and communicate by passing a value of a
|
<a href="#Go_statements">concurrently executing functions</a>
|
||||||
specified element type.
|
to communicate by
|
||||||
|
<a href="#Send_statements">sending</a> and
|
||||||
|
<a href="#Receive_operator">receiving</a>
|
||||||
|
values of a specified element type.
|
||||||
The value of an uninitialized channel is <code>nil</code>.
|
The value of an uninitialized channel is <code>nil</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre class="ebnf">
|
<pre class="ebnf">
|
||||||
ChannelType = ( "chan" [ "<-" ] | "<-" "chan" ) ElementType .
|
ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType .
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The <code><-</code> operator specifies the channel <i>direction</i>,
|
The optional <code><-</code> operator specifies the channel <i>direction</i>,
|
||||||
<i>send</i> or <i>receive</i>. If no direction is given, the channel is
|
<i>send</i> or <i>receive</i>. If no direction is given, the channel is
|
||||||
<i>bi-directional</i>.
|
<i>bidirectional</i>.
|
||||||
A channel may be constrained only to send or only to receive by
|
A channel may be constrained only to send or only to receive by
|
||||||
<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
|
<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
|
||||||
</p>
|
</p>
|
||||||
@ -1318,7 +1321,7 @@ chan (<-chan int)
|
|||||||
A new, initialized channel
|
A new, initialized channel
|
||||||
value can be made using the built-in function
|
value can be made using the built-in function
|
||||||
<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
|
<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
|
||||||
which takes the channel type and an optional capacity as arguments:
|
which takes the channel type and an optional <i>capacity</i> as arguments:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@ -1326,21 +1329,35 @@ make(chan int, 100)
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The capacity, in number of elements, sets the size of the buffer in the channel. If the
|
The capacity, in number of elements, sets the size of the buffer in the channel.
|
||||||
capacity is greater than zero, the channel is asynchronous: communication operations
|
If the capacity is zero or absent, the channel is unbuffered and communication
|
||||||
succeed without blocking if the buffer is not full (sends) or not empty (receives),
|
succeeds only when both a sender and receiver are ready. Otherwise, the channel is
|
||||||
and elements are received in the order they are sent.
|
buffered and communication operations succeed without blocking if the buffer
|
||||||
If the capacity is zero or absent, the communication succeeds only when both a sender and
|
is not full (sends) or not empty (receives).
|
||||||
receiver are ready.
|
|
||||||
A <code>nil</code> channel is never ready for communication.
|
A <code>nil</code> channel is never ready for communication.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
A channel may be closed with the built-in function
|
A channel may be closed with the built-in function
|
||||||
<a href="#Close"><code>close</code></a>; the
|
<a href="#Close"><code>close</code></a>.
|
||||||
multi-valued assignment form of the
|
The multi-valued assignment form of the
|
||||||
<a href="#Receive_operator">receive operator</a>
|
<a href="#Receive_operator">receive operator</a>
|
||||||
tests whether a channel has been closed.
|
reports whether a received value was sent before
|
||||||
|
the channel was closed.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
A single channel may be used in
|
||||||
|
<a href="#Send_statements">send statements</a>,
|
||||||
|
<a href="#Receive_operator">receive operations</a>,
|
||||||
|
and calls to the built-in functions
|
||||||
|
<a href="#Length_and_capacity"><code>cap</code></a> and
|
||||||
|
<a href="#Length_and_capacity"><code>len</code></a>
|
||||||
|
by any number of goroutines without further synchronization.
|
||||||
|
Channels act as first-in-first-out queues.
|
||||||
|
For example, if one goroutine sends values on a channel
|
||||||
|
and a second goroutine receives them, the values are
|
||||||
|
received in the order sent.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
|
<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
|
||||||
@ -3389,7 +3406,8 @@ and the type of the receive operation is the element type of the channel.
|
|||||||
The expression blocks until a value is available.
|
The expression blocks until a value is available.
|
||||||
Receiving from a <code>nil</code> channel blocks forever.
|
Receiving from a <code>nil</code> channel blocks forever.
|
||||||
A receive operation on a <a href="#Close">closed</a> channel can always proceed
|
A receive operation on a <a href="#Close">closed</a> channel can always proceed
|
||||||
immediately, yielding the element type's <a href="#The_zero_value">zero value</a>.
|
immediately, yielding the element type's <a href="#The_zero_value">zero value</a>
|
||||||
|
after any previously sent values have been received.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@ -4238,22 +4256,8 @@ A send on a closed channel proceeds by causing a <a href="#Run_time_panics">run-
|
|||||||
A send on a <code>nil</code> channel blocks forever.
|
A send on a <code>nil</code> channel blocks forever.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
|
||||||
Channels act as first-in-first-out queues.
|
|
||||||
For example, if a single goroutine sends on a channel values
|
|
||||||
that are received by a single goroutine, the values are received in the order sent.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
A single channel may be used for send and receive
|
|
||||||
operations and calls to the built-in functions
|
|
||||||
<a href="#Length_and_capacity"><code>cap</code></a> and
|
|
||||||
<a href="#Length_and_capacity"><code>len</code></a>
|
|
||||||
by any number of goroutines without further synchronization.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
ch <- 3
|
ch <- 3 // send value 3 to channel ch
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
@ -5383,8 +5387,8 @@ make(T, n, m) slice slice of type T with length n and capacity m
|
|||||||
make(T) map map of type T
|
make(T) map map of type T
|
||||||
make(T, n) map map of type T with initial space for n elements
|
make(T, n) map map of type T with initial space for n elements
|
||||||
|
|
||||||
make(T) channel synchronous channel of type T
|
make(T) channel unbuffered channel of type T
|
||||||
make(T, n) channel asynchronous channel of type T, buffer size n
|
make(T, n) channel buffered channel of type T, buffer size n
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user