3 ms·
Channel arrows are basically just read/write operations. If `ch` is a channel, then this expression means "value obtained from reading from the channel":
by bheadmaster 4y ago
Channel arrows are basically just read/write operations.
If `ch` is a channel, then this expression means "value obtained from reading from the channel":
<-ch
And this expression means "write value x into the channel":
ch <- x
Both expressions can be used as a case inside select statement:
select {
case val := <-ch:
// ...
case ch <- val:
// ...
}
Which will execute exactly one case, depending on which channel becomes "ready" first - channel is ready for reading if there is another goroutine blocked on a write operation, and ready for writing if there is another goroutine blocked on a read operation.
You say you're from C background - if you've ever worked will file descriptors you will notice that channels are basically userspace file descriptors. Channel reading and writing is isomorphic to read() and write() syscalls, and select keyword is isomorphic to select() syscall.
Hopefully this clears up the whole channel syntax thing. I just hope you weren't trolling.
- morelisp 4y ago> Channel reading and writing is isomorphic to read() and write() syscalls This is a very bad mental model because channels operations cannot be canceled (without using select on two channels) or return any error status (at all).
- paskozdilar 4y agoWhile technically true, I don't really see the impact of the difference. It is idiomatic to use contexts for any kind of cancellation during channel operations, and it works well. The syscall comparison was made to give intuition about general behavior of channels to someone with a background in C. Of course it's not completely identical.
- morelisp 4y agoThe ability of read/write to communicate via errors as well as the actual data transferred is significant - there's a reason Go's i/o model is io.Reader/io.Writer and not chan []byte. You might as well explain channels in terms of any blocking operation if the bar for "isomorphic" (now backtracked to "intuitively" I guess) is that low.
- paskozdilar 4y agoI don't know what's the bar for "isomorphism", but I know that the word literally means "same shape", so just because some nerd that got killed in a duel over a girl used the same word in a mathematical context doesn't give him dibs over its general use. Unless, of course, system calls can be modeled as operators over sets. In which case, please tell me how.
- morelisp 4y agoIn normal use of the term, a function's shape is its type signature; at the very least its in and out arity, and usually even more specific.
- paskozdilar 4y agoWell, in my case I used it to signify same behavior in terms of process/goroutine communication. I think it's "specific enough" to warrant the use of word "isomorphic".
- moffkalast 4y agoIt was a rhetorical question.
- paskozdilar 4y agoSo you were trolling. Bummer.