Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Go, what does channel assgiment mean?

Tags:

go

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

like image 696
BufBills Avatar asked Dec 30 '25 05:12

BufBills


1 Answers

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.

like image 157
VonC Avatar answered Dec 31 '25 17:12

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!