是的,有可能。例如,在您的代码中,您可以使用:
greet: make(chan pet)
然后,您将能够无缝发送任何实现的内容
type pet interface。
如果您想发送 完全 通用的内容,可以使用a
chan interface{},然后reflect在收到内容时使用它来查找内容。
愚蠢的-可能不是惯用的-例如:
ch := make(chan interface{})go func() { select { case p := <-ch: fmt.Printf("Received a %q", reflect.TypeOf(p).Name()) }}() ch <- "this is it"正如BurntSushi5指出的那样,类型开关更好:
p := <-chswitch p := p.(type) {case string: fmt.Printf("Got a string %q", p)default: fmt.Printf("Type of p is %T. Value %v", p, p)}


