<- <- vs. <- what is the difference?

Why would you use “addedStream <- i + additive” vs. “case addedStream <- <- i + additive”

Thanks in advanced! It is hard to duckduckgo symbols!

This is the code that compiles:

take := func(
	done <-chan interface{},
	valueStream <-chan interface{},
	num int,
) <-chan interface{} {
	takeStream := make(chan interface{})
	go func() {
		defer close(takeStream)
		for i := 0; i < num; i++ {
			select {
			case <-done:
				return
			case takeStream <- <-valueStream:
			}
		}

	}()
	return takeStream

}

addedStream <- i + additive

This computes i + additive and sends it to the addedStream channel.

case addedStream <- <- i + additive

This does not compile (at least not as is, and assuming the same types as implied by the first snippet). If there were just one arrow operator it would mean the same thing as above, but presumable in a select clause (given the case).

Assuming you had two channels, one you can send to and one you can receive from, you could do

sendChannel <- <-recvChannel

which means to receive one value from recvChannel (<-recvChannel) and send it to the sendChannel (sendChannel <- ...). If both the channels are chan int you could then imagine

sendChannel <- <-recvChannel + someInt

which is close to your initial example, and clearer understood as

sendChannel <- ((<-recvChannel) + someInt)
2 Likes

Yeah, I’m beginning to figure it out. I’m working through “concurrency in go” and this comes from pg 110. When I use just sendChannel <- recvChannel I get what appears to be a pointer? “0xc420096060” vs.
sendChannel <- <-recvChannel I get the actual value. Thanks for tip.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.