3

i have this code,

// The prime sieve: Daisy-chain Filter processes.
func main() {
    ch := make(chan int) // Create a new channel.
    go Generate(ch)      // Launch Generate goroutine.
    for i := 0; i < 10; i++ {
        prime := <-ch
        print(prime, "\n")
        ch1 := make(chan int)
        go Filter(ch, ch1, prime)
        ch = ch1
    }
}

I am trying to understand what does channel assignment mean. For example ch=ch1, what does this do? Deep copy or shallow copy? what does go guarantee for this?

Thanks

BufBills
  • 8,005
  • 12
  • 48
  • 90

1 Answers1

11

A channel is a reference type. See "Are channels passed by reference implicitly".
(reference types: slices, maps, channels, pointers, functions)
And see "Go - Pointer to map".

ch = ch1 simply copy the reference value of ch1 to ch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250