Reputation: 8443
type Stuff {
ch chan int
}
versus
type Stuff {
ch *chan int
}
I know that channels are reference types & thus is mutable when returned by functions or as arguments. When is an address of a channel useful in a real world program ?
Upvotes: 2
Views: 238
Reputation: 166614
Perhaps your channel is used for rotating logs and you want to rotate (swap) logs; swap channel (log) pointers not values.
For example,
package main
import "fmt"
func swapPtr(a, b *chan string) {
*a, *b = *b, *a
}
func swapVal(a, b chan string) {
a, b = b, a
}
func main() {
{
a, b := make(chan string, 1), make(chan string, 1)
a <- "x"
b <- "y"
swapPtr(&a, &b)
fmt.Println("swapped")
fmt.Println(<-a, <-b)
}
{
a, b := make(chan string, 1), make(chan string, 1)
a <- "x"
b <- "y"
swapVal(a, b)
fmt.Println("not swapped")
fmt.Println(<-a, <-b)
}
}
Output:
swapped
y x
not swapped
x y
Upvotes: 1