What is the <-
operator in go language? Have seen this in many code snippets related to Go but what is the meaning of it?
<-
is used in more than one place in the language specification:
Channel types:
The
<-
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<-ch
is the value received from the channelch
. 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