学习 golang ,对于 interface{} 接口类型,我们一定绕不过,咱们一起来看看 使用 interface{} 的时候,都有哪些注意事项吧
xdm 咱们写一个简单的例子,就举动物的例子
写一个 Animal 的接口,类似于 java 里面的抽象类 ,Animal 的接口 中有 2 个方案待实现
写一个 Cat 来继承 Animal , 实现 Eat 方法和 Drink 方法
type Animal interface {Eat(string) stringDrink(string) string
}type Cat struct{}func (c *Cat) Eat(food string) string {if food != "fish" {return "i dislike"} else {return "i like"}}
func (c *Cat) Drink(drink string) string {if drink == "coke" {return "i love"}else{return "abandon"}
}
func main(){var a Animal = &Cat{}fmt.Println(a.Eat("fish"))fmt.Println(a.Drink("water"))
}
看到上述代码,会不会有这样的疑问,命名是 &Cat{}
是取地址的,为什么 var a Animal
不写成指针呢?
这里需要注意,Animal 本身是 接口类型,自身就是一个指针
运行上述代码查看效果
# go run main.go
i like
abandon
没有毛病,小猫眯爱吃鱼,不爱喝水
什么叫做空的 interface{} , 什么又叫做非空的 interface{} 呢?
咱们还是用上面的例子, 添加一个 testInterface 函数
,来实践一下
func testInterface() Animal {var c *Catreturn c
}func main() {test := testInterface()if test == nil {fmt.Println("test is nil")} else {fmt.Println("test is not nil")}
}
可以猜猜看,上面这个小案例会输出什么结果
testInterface
函数中我们只是创建了一个 Cat 指针,并没有赋值,因此默认是一个零值,因此会是一个 nil,那么 return 的时候,应该也是 return nil 才对吧,因此按照代码的逻辑来说应该是输出 test is nil
执行上述代码后,查看结果
# go run main.go
test is not nil
看到上面的结果,是不是觉得很奇怪,和自己的预期不一致
没关系,之前的文章我们说到过,觉得一个技术点奇怪,不是我们所期望的效果,原因是我们对其原理不够了解,不够熟悉
现在先来回答一下上面的问题
空接口:意思是没有方法的接口,interface{} 源码中表示为 eface 结构体
非空接口:表示有包含方法的接口 , interface{} 源码中表示为 iface 结构体
暂时先来直接介绍源码中的结构体
type iface struct {tab *itabdata unsafe.Pointer
}type itab struct {inter *interfacetype_type *_typelink *itabhash uint32 // copy of _type.hash. Used for type switches.bad bool // type does not implement interfaceinhash bool // has this itab been added to hash?unused [2]bytefun [1]uintptr // variable sized
}
tab
指的是具体的类型信息,是一个 itab 结构,结构中成员如上,这里面包含的都是借口的关键信息,例如 hash 值 ,函数指针,等等,后续详细剖析 interface{} 原理的时候再统一说
data
具体的数据信息
type eface struct {_type *_typedata unsafe.Pointer
}
type _type struct {size uintptr // 表示的是 类型的大小ptrdata uintptr // 值的是前缀指针的内存大小hash uint32 // 计算数据的 hash 值tflag tflagalign uint8 // 进行内存对齐的fieldalign uint8 kind uint8 alg *typeAlg gcdata *bytestr nameOffptrToThis typeOff
}
类型信息,和上面的 非空接口类似 , 这个_type 类型决定下面的 data 字段如何去解释数据
具体的数据信息
看到这里,细心的 xdm 是不是就可以看出来,我们上面写的 Animal 接口,其实是一个非空接口,因为里面有包含方法,所以他的底层是一个 iface 结构体 ,非空接口
那么初始化的一个空指针 c ,实际上是 iface 结构体里面的 data 字段为空而已,数据为空而已,但是 iface 这个结构体自己不是空的,所以上述代码走的逻辑是 test is not nil
这里顺带说一下,golang 中,还有哪些数据结构是和 nil 比较是否为零值,这个点我们也可以看看源码
// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
源码中有说到,可以对 指针,通道,函数,接口,map,切片类型使用 nil
好了,本次就到这里,知识点要用起来才有价值
朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力
好了,本次就到这里
技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。
我是阿兵云原生,欢迎点赞关注收藏,下次见~
上一篇:【JAVA】Java中集合的遍历
下一篇:200 1111