什么是“少于随后的破折号”运用语言? [英] what is the "less than followed by dash" operator in go language?

查看:115
本文介绍了什么是“少于随后的破折号”运用语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

但在这里。



将频道视为消息队列。



如果频道位于右侧左手箭头(< ;-)运营商,它意味着出列一个条目。将条目保存在变量中是可选的

  e < -  q 
pre>

如果频道位于左箭头运算符的左侧,则意味着将条目排入队列。

 q < -  e 

关于出队 (接收)而不存储在变量中:它可以在非缓冲队列中使用,以便在Java中实现类似等待/通知的操作:一个协程被阻塞等待出队/接收信号,然后另一个协程入队/发送该信号,其内容不重要。 (或者,发件人可能会被阻止,直到接收者拉出消息)

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

解决方案

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 <- q

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

q <- 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)

这篇关于什么是“少于随后的破折号”运用语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆