mirror of
https://github.com/golang/go
synced 2024-11-22 02:34:40 -07:00
go spec: simplify section on channel types
R=rsc, iant, r CC=golang-dev https://golang.org/cl/1171041
This commit is contained in:
parent
f023e859cf
commit
56ca697269
@ -1,5 +1,5 @@
|
|||||||
<!-- title The Go Programming Language Specification -->
|
<!-- title The Go Programming Language Specification -->
|
||||||
<!-- subtitle Version of May 4, 2010 -->
|
<!-- subtitle Version of May 7, 2010 -->
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Todo
|
Todo
|
||||||
@ -1154,16 +1154,26 @@ A value of channel type may be <code>nil</code>.
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre class="ebnf">
|
<pre class="ebnf">
|
||||||
ChannelType = Channel | SendChannel | RecvChannel .
|
ChannelType = ( "chan" [ "<-" ] | "<-" "chan" ) ElementType .
|
||||||
Channel = "chan" ElementType .
|
|
||||||
SendChannel = "chan" "<-" ElementType .
|
|
||||||
RecvChannel = "<-" "chan" ElementType .
|
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
To avoid a parsing ambiguity in cases such as <code>chan<- chan int</code>,
|
The <code><-</code> operator specifies the channel <i>direction</i>,
|
||||||
the Channel production's ElementType cannot be a RecvChannel.
|
<i>send</i> or <i>receive</i>. If no direction is given, the channel is
|
||||||
To construct such a type, parenthesize the RecvChannel first.
|
<i>bi-directional</i>.
|
||||||
|
A channel may be constrained only to send or only to receive by
|
||||||
|
<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
chan T // can be used to send and receive values of type T
|
||||||
|
chan<- float // can only be used to send floats
|
||||||
|
<-chan int // can only be used to receive ints
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The <code><-</code> operator associates with the leftmost <code>chan</code>
|
||||||
|
possible:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@ -1173,22 +1183,10 @@ chan<- <-chan int // same as chan<- (<-chan int)
|
|||||||
chan (<-chan int)
|
chan (<-chan int)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
|
||||||
Upon creation, a channel can be used both to send and to receive values.
|
|
||||||
By conversion or assignment, a channel may be constrained only to send or
|
|
||||||
to receive. This constraint is called a channel's <i>direction</i>; either
|
|
||||||
<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<pre>
|
|
||||||
chan T // can be used to send and receive values of type T
|
|
||||||
chan<- float // can only be used to send floats
|
|
||||||
<-chan int // can only be used to receive ints
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
|
The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
|
||||||
value can be made using the built-in function <code>make</code>,
|
value can be made using the built-in function
|
||||||
|
<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 capacity as arguments:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user