首页 > 其他分享 >golang:关于interface{}==nil 的坑及原理分析

golang:关于interface{}==nil 的坑及原理分析

时间:2023-01-02 15:02:39浏览次数:44  
标签:struct nil fmt ts golang 类型 interface type

先看一个demo

package main

import "fmt"
type TestStruct struct{}

var ts *TestStruct
func getTestStruct() interface{} {
fmt.Println(ts == nil)
return ts
}
func main() {
buf := getTestStruct()
fmt.Println(buf == nil)
}

输出结果:

true
false

为什么不是true,true?

对于这个问题需要理解interface的本质,对于无方法的interface,也叫空接口,go内部通过eface结构体定义实现,位于src/runtime/runtime2.go

type eface struct{
_type *_type //类型信息指针
data unsafe.Pointer //数据信息指针
}

可以看到上面eface包含了2个元素,一个是_type,指向对象的类型信息,一个 data,指向数据的指针。

所以原因显而易见了,就是因为代码中的ts返回时候,只是把eface的data赋值了,但是eface不为nil。

再深入一层,看下_type的定义,位于src/runtime/type.go

type _type struct {
size uintptr // 数据类型共占用的空间大小
ptrdata uintptr // 含有所有指针类型前缀大小
hash uint32 // 类型hash值
tflag tflag // 额外类型信息标志
align uint8 // 该类型变量对齐方式
fieldAlign uint8 // 该类型结构字段对齐方式
kind uint8 // 类型编号
equal func(unsafe.Pointer, unsafe.Pointer) bool //判断类型是否一致的函数
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte // gc数据
str nameOff //类型名字的偏移
ptrToThis typeOff
}

但是每个类型需要的类型描叙是不一样的,比如chan,slice,除了本身外,还需要描述其元素类型等

type arraytype struct {
typ _type
elem *_type
slice *_type
len uintptr
}

type chantype struct {
typ _type
elem *_type
dir uintptr
}

type slicetype struct {
typ _type
elem *_type
}

type functype struct {
typ _type
inCount uint16
outCount uint16
}

type ptrtype struct {
typ _type
elem *_type
}

type structtype struct {
typ _type
pkgPath name
fields []structfield
}

可以发现,上面的类型信息,第一个都是_type,然后后面是一堆额外信息;在进行类型操作的时候,通过第一个_type表述类型,然后通过_type.kind解析出具体的类型,然后通过地址转化,得到完整的type。

如何解决true,flase的问题?

  1. 返回具体的类型,而不是返回interface
package main

import "fmt"
type TestStruct struct{}

var ts *TestStruct
func getTestStruct() *TestStruct {
fmt.Println(ts == nil)
return ts
}
func main() {
buf := getTestStruct()
fmt.Println(buf == nil)
}

2. 返回结果时进行非nil检查,然后再赋值给interface{}变量

package main

import "fmt"
type TestStruct struct{}

var ts *TestStruct
func getTestStruct() interface{} {
fmt.Println(ts == nil)
if ts == nil{
return nil
}
return ts
}
func main() {
buf := getTestStruct()
fmt.Println(buf == nil)
}

拓展一下interface

interface还存在一种非空接口

// runtime/runtime2.go
type iface struct {
tab *itab
data unsafe.Pointer //指向原始数据指针
}

itab是接口的核心,发音为i-table,源于c语言中组成接口的Itab;

type itab struct {
inter *interfacetype
_type *_type
hash uint32 // copy of _type.hash. Used for type switches.
_ [4]byte
fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
}

其中_type字段代表接口存储的动态类型,inter代表接口本身的类型,是对_type的简单包装

type interfacetype struct {
typ _type
pkgpath name // 接口所在的包名
mhdr []imethod // 接口中暴露的方法在最终可执行文件中的名字和类型的偏移量
}

而itab中的hash是对_type类型中hash的拷贝,在接口类型断言时,该字段快速判断接口动态类型与具体类型_type是否一致;一个空的_4字节用于内存对齐,最后的fun字段代表接口动态类型中的函数指针列表,用于运行时接口调用动态函数。

大概图示如下

golang:关于interface{}==nil 的坑及原理分析_数据

interface内存

很容易理解,data存储的数据是指针,数据可以很大,也可以很小,难以预料。所以平时分配在栈中的值一旦赋值给接口后,会发生内存逃逸,在堆区为其开辟内存。

标签:struct,nil,fmt,ts,golang,类型,interface,type
From: https://blog.51cto.com/u_15916656/5983713

相关文章