logoalt Hacker News

LukeShuyesterday at 9:45 AM2 repliesview on HN

"<-chan TYPE" is the receive end, "chan<- TYPE" is the send end, "chan TYPE" without an arrow can be used for either.

close() on a chan is "indicate end-of-transmission"; you should only ever use it from the send end. There is no way to explicitly "close" the receive end.

You can use `select` to do a non-blocking sends/receives, but yeah normal sends/receives are blocking.


Replies

masklinnyesterday at 12:19 PM

> "<-chan TYPE" is the receive end, "chan<- TYPE" is the send end, "chan TYPE" without an arrow can be used for either.

The problem is that there is pretty much just a subtyping relationship, when you convert a channel to a send or receive channel you don't get a different object, you just get the relevant subset of operations.

> There is no way to explicitly "close" the receive end.

And that's the root cause of half of more of the example issues in TFA. With the ability to close either end of the channel (and to handle closed receivers from the senders, obviously), most of the issues just go away (even more reliably so if that happens for you when the relevant end stops being used).

mitxelayesterday at 1:36 PM

It's still the same object pointer. The GC can't force close a channel when it has no readable references left.