这显然有效:
// cast chan string to <-chan string
func RecOnly(c chan string) <-chan string {
return c
}
func main() {
a := make(chan string, 123)
b := RecOnly(a)
a <- "one"
a <- "two"
//b <- "beta" // compile error because of send to receive-only channel
fmt.Println("a", <-a, "b", <-b)
}
但是有没有一个单线来做到这一点,而不声明一个新功能?
aluckdog
相关分类