BufBills
BufBills

Reputation: 8103

in Go, what does channel assgiment mean?

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

Upvotes: 3

Views: 2006

Answers (1)

VonC
VonC

Reputation: 1327584

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.

Upvotes: 12

Related Questions