Concurrency confusion

Not sure what is the meaning of these in Go:

channel1 = channel2 (channel2 adding to channel1 or channel1 is now channel2 ?!)

or

channel1 = <- channel2 (what’s the difference between this and channel1 <- channel2 or channel1 = channel2)

1 Like

Assignment has two parts, the left hand side of the equals sign, and the right hand side.

Assignment evaluates the arguments on the right hand side, RHS and places the result into the left hand side.

Sometimes things evaluate to themselves

var x int
x = 2

The number 2 evaluates to itself, 2.

var x, y int
x = 1
y = x

In this example, y = x, x is on the right hand side, so it’s evaluated, and the results are placed in y. The result of evaluating x is the contents of x, 1. This is why everything in Go is a copy.

So, with this knowledge, let’s look at your question again

channel1 = channel2

Evaluates channel2, which is a variable, and assigns the results, the contents of channel2, a value some chan type to `channel1.

Now, the final case

channel1 = <- channel2

We have to do a bit more work to fully evaluate the RHS. First we have to evaluate channel2 to get a value of some chan type, then we evaluate the using the receive operator <- on the value of channel2. Finally the result of this receive operation are assigned to channel1.

6 Likes

Just to add to @dfc’s great explanation:
Since chans are just variables containing the information to use a channel, when they are copied (even though it’s “by value”), they are pointing to the same channel, similar to how slices and maps behave.
So in the example above, channel1 and channel2 are two different variables pointing to the same channel; using one would be like using the other.
See https://play.golang.org/p/062rthmJLa for illustration.

1 Like

Great illustration.
How channel1 = <- channel2 is different from channel1 = channel2 ?

Hey @irom77,

@dfc’s post explains this pretty well, so I’m not sure if you fully read or understood the post, but here’s the simplest terms that I can think of to explain your question for somebody that understands what a channel in Go is:

channel1 = channel2 is assigning channel1 to the same chan as channel2.
channel1 = <-channel2 is assigning channel1 to the first value that is received off of channel2.

So the answer the first thing you asked: [quote=“irom77, post:1, topic:4677”]
channel1 = channel2 (channel2 adding to channel1 or channel1 is now channel2 ?!)
[/quote]

channel1 is now “the same” as channel2.

2 Likes

Appreciate, of course I am reading all answers. I think I got it. Now I can see the difference between two (how channel1 = <- channel2 is different from channel1 = channel2), … hm, but now how channel1 = <- channel2 is different from channel1 <- channel2 ?

Hey @irom77,

Saying channel1 <- channel2 means to take the first value received from the channel channel2 and send it into the channel channel1.

It would be similar to using this:

val := <-channel2 // Receive a value from channel2 and store it in val.
channel1 <- val // Put the value stored in val into channel1.

thnx again. so

  1. channel1 = <-channel2 is assigning channel1 to the first value that is received off of channel2
  2. channel1 <- channel2 means to take the first value received from the channel channel2 and send it into the channel channel1.

Understood, probably just need to find more examples where these two have to be used. I was only using this simple val <-channel2 and channel1 <- val or <-val etc pattern so far

Just keep in mind, with the example channel1 = <-channel2, channel1 is a bad variable name to use since you can’t actually store a regular value in a channel like that. Normally it would be named with something like val = <-channel2.

Edit: I actually made a mistake, channel1 <- channel2 is not what I meant to say, channel1 <- <-channel2 is what I was referring to.

1 Like

As for my previous typo mistake, here’s a very small example set:

package main

import "fmt"

func main() {
	channel1 := make(chan int, 1)
	channel2 := make(chan int, 1)

	// Put a value in channel1.
	channel1 <- 1

	// Receive a value from channel1.
	val := <-channel1

	fmt.Println(val) // Print the received value.

	// Put a value into channel2.
	channel2 <- 2

	// Receive a value from channel2 and send it into channel1.
	channel1 <- <-channel2

	// Receieve the value from channel1.
	fmt.Println(<-channel1)
}

I wouldn’t worry about using channel1 <- <- channel2 for now, but as for channel1 <- val, that’s just to regularly put a value into a channel.

Here is the same example as above, but using 2 variables instead of the 1 and 2 being passed in the channels.

package main

import "fmt"

func main() {
	one := 1
	two := 2

	channel1 := make(chan int, 1)
	channel2 := make(chan int, 1)

	// Put a value in channel1.
	channel1 <- one

	// Receive a value from channel1.
	val := <-channel1

	fmt.Println(val) // Print the received value.

	// Put a value into channel2.
	channel2 <- two

	// Receieve a value from channel2 and send it into channel1.
	channel1 <- <-channel2

	// Receieve the value from channel1.
	fmt.Println(<-channel1)
}
1 Like

Right, this is where my confusion started, seeing channel keyword in the variable name, my bad :slight_smile:

One last thing, since I actually realize you probably are actually referring to using this channel1 <- channel2 as 2 channels and not as a variable name, this is actually referring to sending a channel over another channel, for example:

package main

import "fmt"

func main() {
	channel1 := make(chan chan int, 1) // chan of chan of int.
	channel2 := make(chan int, 1)      // chan of int.

	// Send a chan of int into a channel.
	channel1 <- channel2

	// Receive a chan of int off channel1. (channel2)
	ch2 := <-channel1

	// Send a value into ch2.
	ch2 <- 10

	// Since ch2 is the "same" channel as channel2, receive
	// and print the value off of channel2.
	fmt.Println(<-channel2)
}

It can be confusing, I know, but you will rarely need to use something like this, so probably best not to even worry about it until you need something like it.

2 Likes

I like that example, as soon I can see chan chan makes sense for me

1 Like

Yeah, sorry, I completely missed that part of what you were asking originally and just assumed you meant a regular variable assignment, but it should make better sense now :slight_smile:

1 Like

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