广告
返回顶部
首页 > 资讯 > 后端开发 > GO >详解Golang中interface{}的注意事项
  • 850
分享到

详解Golang中interface{}的注意事项

摘要

目录interface {} 可以用于模拟多态interface{} 需要注意空和非空的情况iface结构体,非空eface结构体interface {} 可以用于模拟多态 xdm

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

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

以上就是详解Golang中interface{}的注意事项的详细内容,更多关于Golang interface的资料请关注编程网其它相关文章!

您可能感兴趣的文档:

--结束END--

本文标题: 详解Golang中interface{}的注意事项

本文链接: https://www.lsjlt.com/news/198999.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作