在下面的代码中
var r io.Reader
c, _ := net.Dial("tcp", ":8080")
r = c
switch r.(type) {
case io.Reader:
fmt.Println("r implements the reader interface")
// fallthrough
case io.Writer:
fmt.Println("r implements the writer interface")
case net.Conn:
fmt.Println("r implements the conn interface")
}
第一个case语句是始终执行的语句。
如果我取消注释fallthrough代码将无法编译,因为fallthrough类型开关中不允许这样做
如果我没记错,go接口有 2 个值:
存储在接口中的实际类型(描述符)(例如io.Writer,io.Reader等)
存储在其中的值
既然r显然满足上述所有case陈述,那么找到存储在中的确切类型的方法是什么r?
是不是只能通过反射才能完成这个锥体?
慕斯王
FFIVE
相关分类