Learning channels and reading the documentation

Documentation:

Channel Types

The optional <- operator specifies the channel direction, send or receive. If no direction is given, the channel is bidirectional. A channel may be constrained only to send or only to receive by assignment or explicit conversion.
 
chan T          // can be used to send and receive values of type T
chan<- float64  // can only be used to send float64s
<-chan int      // can only be used to receive ints
The <- operator associates with the leftmost chan possible:
 
chan<- chan int    // same as chan<- (chan int)
chan<- <-chan int  // same as chan<- (<-chan int)
<-chan <-chan int  // same as <-chan (<-chan int)
chan (<-chan int)

In Rob Pikes code:

The <- operator associates with the leftmost chan possible (Documentation)

Why are we using c <- <- input1 for receiving? c is sending c <- and receiving <-input1?

chan<- (<-chan int) or c <- (<- input1) is the equivalent, but when i read this from left to right out loud it does not make sense. c is sending c <- and receiving <-input1? Do the <- <- cancel each other and default to the left most channel receiving?

I understand the cis a bidirectional channel so that it handles conversion for both receiving and sending, but for some reason I am having a hard time seeing the logical pattern here.

c <- <- input1 reads a value from input1 and writes it to c. It would be more clearly written less tersely as

  value := <-input
  c <- value
2 Likes

Thanks Jeff, I really appreciate the quick response!

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