关于go:关于-interface-会有啥注意事项上

4次阅读

共计 2542 个字符,预计需要花费 7 分钟才能阅读完成。

学习 golang,对于 interface{} 接口类型,咱们肯定绕不过,咱们一起来看看 应用 interface{} 的时候,都有哪些注意事项吧

interface {} 能够用于模仿多态

xdm 咱们写一个简略的例子,就举动物的例子

写一个 Animal 的接口,相似于 java 外面的抽象类,Animal 的接口 中有 2 个计划待实现

写一个 Cat 来继承 Animal实现 Eat 办法和 Drink 办法

  • 动物都有吃和喝的行为,小猫吃的行为是吃鱼,小猫的喝的行为是喝可乐
  • 最初在主函数中,应用父类的指针,来指向子类的实例化的一个子类地址
type Animal interface {Eat(string) string
    Drink(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{},什么又叫做非空的 interface{} 呢?

咱们还是用下面的例子,增加一个 testInterface 函数,来实际一下

func testInterface() Animal {
    var c *Cat
    return 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 构造体

临时先来间接介绍源码中的构造体

iface 构造体,非空

type iface struct {
    tab  *itab
    data unsafe.Pointer
}

type itab struct {
    inter  *interfacetype
    _type  *_type
    link   *itab
    hash   uint32 // copy of _type.hash. Used for type switches.
    bad    bool   // type does not implement interface
    inhash bool   // has this itab been added to hash?
    unused [2]byte
    fun    [1]uintptr // variable sized
}
  • tab

    指的是具体的类型信息,是一个 itab 构造,构造中成员如上,这外面蕴含的都是借口的要害信息,例如 hash 值,函数指针,等等,后续具体分析 interface{} 原理的时候再对立说

  • data

具体的数据信息

eface 构造体

type eface struct {
    _type *_type
    data  unsafe.Pointer
}
type _type struct {
    size       uintptr  // 示意的是 类型的大小
    ptrdata    uintptr  // 值的是前缀指针的内存大小
    hash       uint32   // 计算数据的 hash 值
    tflag      tflag
    align      uint8    //  进行内存对齐的
    fieldalign uint8 
    kind       uint8 
    alg        *typeAlg 
    gcdata    *byte
    str       nameOff
    ptrToThis typeOff
}
  • _type

类型信息,和下面的 非空接口相似,这个_type 类型决定上面的 data 字段如何去解释数据

  • 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

好了,本次就到这里,知识点要用起来才有价值

欢送点赞,关注,珍藏

敌人们,你的反对和激励,是我保持分享,提高质量的能源

好了,本次就到这里

技术是凋谢的,咱们的心态,更应是凋谢的。拥抱变动,背阴而生,致力向前行。

我是 阿兵云原生,欢送点赞关注珍藏,下次见~

正文完
 0