I'm a newbie in Go, so sorry if my question seems stupid.
I have a really hard time to get the channel syntax and vocabulary in Golang, especially when it's about direction.
When I read that c chan<- int
is a "send-only" channel, I understand that this channel can only send data. But it seems to be the contrary, and that we can only put stuff into this chan.
Same thing for the "receive-only" channel c <-chan int
: for me it's a channel that can only receive data, but it seems this kind of channel can only send data and not receive some.
See, I'm really confused, so if you have a better way to explain it, or a good mnemonic way to understand it, I'll thank you for the rest of my days!
The channel cannot send nor receive data. You can send data on the channel, and you can receive data from the channel. And then the direction is exactly what its name says.
Also note that the "arrow" visualizes the direction. If it points toward the channel (toward the chan
) like c chan<- int
, it's a send-only. If it points away from the channel (outward of the chan
) like c <-chan int
, it's a receive-only.
Same goes for actually sending anything on it (Send statement) like c <- 1
, or receiving something from it (Receive operator) like a = <-c
.