什么是Golang中的“小于后跟破折号”(<-)运算符?

huangapple go评论80阅读模式
英文:

What is the "less than followed by dash" (<-) operator in Golang?

问题

什么是Go语言中的<-运算符?在许多与Go相关的代码片段中看到过这个运算符,但它的含义是什么?

英文:

What is the &lt;- operator in go language? Have seen this in many code snippets related to Go but what is the meaning of it?

答案1

得分: 27

你已经得到了答案,但是我还是给你解释一下。

将通道(channel)看作是一个消息队列。

如果通道在左箭头(<-)操作符的右边,意味着从队列中取出一个条目。将该条目保存在一个变量中是可选的。

e <- q

如果通道在左箭头操作符的左边,意味着将一个条目加入队列。

q <- e

关于“取出”(接收)操作而不将其存储在变量中的进一步说明:它可以用于非缓冲队列来实现类似于Java中的“等待/通知”操作:一个协程被阻塞,等待从队列中取出/接收一个信号,然后另一个协程将该信号加入/发送到队列中,其中信号的内容并不重要。(或者,发送者可以被阻塞,直到接收者取出消息)

英文:

You've already got answers, but here goes.

Think of a channel as a message queue.

If the channel is on the right of the left arrow (<-) operator, it means to dequeue an entry. Saving the entry in a variable is optional

e &lt;- q

If the channel is on the left of the left arrow operator, it means to enqueue an entry.

q &lt;- e

Further note about "dequeue" (receive) without storing in a variable: it can be used on a non-buffered queue to implement something like a "wait/notify" operation in Java: One coroutine is blocked waiting to dequeue/receive a signal, then another coroutine enqueues/sends that signal, the content of which is unimportant. (alternately, the sender could be blocked until the receiver pulls out the message)

答案2

得分: 17

&lt;- 在语言规范中的多个地方使用:

通道类型:

&lt;- 运算符指定通道的方向,发送或接收。如果没有指定方向,则通道是双向的。通过转换或赋值,可以将通道限制为仅发送或仅接收。

接收运算符:

对于通道类型的操作数 ch,接收操作 &lt;-ch 的值是从通道 ch 接收到的值。该值的类型是通道的元素类型。该表达式会阻塞,直到有值可用。从空通道接收会永远阻塞。从已关闭的通道接收总是成功的,立即返回元素类型的零值。

发送语句:

发送语句将一个值发送到通道。通道表达式必须是通道类型,值的类型必须可赋值给通道的元素类型。

SendStmt = Channel "&lt;-" Expression .
Channel  = Expression .

接收运算符也是 select 语句 的基本组成部分。

英文:

&lt;- is used in more than one place in the language specification:

>Channel types:
>
> The &lt;- operator specifies the channel direction, send or receive. If no direction is given, the channel is bi-directional. A channel may be constrained only to send or only to receive by conversion or assignment.
>
>Receive operator:
>
> For an operand ch of channel type, the value of the receive operation &lt;-ch is the value received from the channel ch. The type of the value is the element type of the channel. The expression blocks until a value is available. Receiving from a nil channel blocks forever. Receiving from a closed channel always succeeds, immediately returning the element type's zero value.
>
>Send statements:
>
> A send statement sends a value on a channel. The channel expression must be of channel type and the type of the value must be assignable to the channel's element type.
>
> SendStmt = Channel "<-" Expression .
> Channel = Expression .

The receive operator is also a fundamental part of the select statement

答案3

得分: 3

接收操作符

对于通道类型的操作数ch,接收操作<-ch的值是从通道ch接收到的值。

它从通道接收一个值。请参阅http://golang.org/ref/spec#Receive_operator

英文:

> Receive operator
>
> For an operand ch of channel type, the value of the receive operation
> &lt;-ch is the value received from the channel ch.

It receives a value from a channel. See http://golang.org/ref/spec#Receive_operator

huangapple
  • 本文由 发表于 2013年4月10日 21:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/15926697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定