what is the “less than followed by dash” operator in go language?

后端 未结 3 470
臣服心动
臣服心动 2021-02-03 21:45

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

3条回答
  •  情书的邮戳
    2021-02-03 22:01

    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)

提交回复
热议问题