这篇文章主要介绍了golang interface如何判断为空nil的实现代码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
golang是一种编译语言,可以将代码编译为机器代码,编译后的二进制文件可以直接部署到目标机器而无需额外的依赖,所以golang的性能优于其他的解释性语言,且可以在golang中使用goroutine来实现并发性,它提供了一个非常优雅的goroutine调度程序系统,可以很容易地生成数百万个goroutine。
要判断interface 空的问题,首先看下其底层实现。
根据 interface 是否包含有 method,底层实现上用两种 struct 来表示:iface 和 eface。eface表示不含 method 的 interface 结构,或者叫 empty interface。
对于 Golang 中的大部分数据类型都可以抽象出来 _type 结构,同时针对不同的类型还会有一些其他信息。
type eface struct {
_type *_type
data unsafe.Pointer
}
type _type struct {
size uintptr // type size
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32 // hash of type; avoids computation in hash tables
tflag tflag // extra type information flags
align uint8 // alignment of variable with this type
fieldalign uint8 // alignment of struct field with this type
kind uint8 // enumeration for C
alg *typeAlg // algorithm table
gcdata *byte // garbage collection data
str nameOff // string form
ptrToThis typeOff // type for pointer to this type, may be zero
}
iface 表示 non-empty interface 的底层实现。相比于 empty interface,non-empty 要包含一些 method。method 的具体实现存放在 itab.fun 变量里。如果 interface 包含多个 method,这里只有一个 fun 变量怎么存呢?这个下面再细说。
type iface struct {
tab *itab
data unsafe.Pointer
}
// layout of Itab known to compilers
// allocated in non-garbage-collected memory
// Needs to be in sync with
// ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
type itab struct {
inter *interfacetype
_type *_type
link *itab
bad int32
inhash int32 // has this itab been added to hash?
fun [1]uintptr // variable sized
}
概括起来,接口对象由接口表 (interface table) 指针和数据指针组成,或者说由动态类型和动态值组成。
struct Iface
{
Itab* tab;
void* data;
};
struct Itab
{
InterfaceType* inter;
Type* type;
void (*fun[])(void);
};
接口表存储元数据信息,包括接口类型、动态类型,以及实现接口的方法指针。无论是反射还是通过接口调用方法,都会用到这些信息。
再来看下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
也就是说,只有pointer, channel, func, interface, map, or slice 这些类型的值才可以是nil.
对于一个接口的零值就是它的类型和值的部分都是nil。
一个接口值基于它的动态类型被描述为空或非空。
例如,
var w io.Writer
一般情况下,通过使用w==nil或者w!=nil来判读接口值是否为空,只是判断了动态类型,而没有判断动态值。
例如,下面的例子。
package main
import ("fmt")
func main(){
var a interface{} = nil // tab = nil, data = nil
var b interface{} = (*int)(nil) // tab 包含 *int 类型信息, data = nil
fmt.Println(a==nil)
fmt.Println(b==nil)
}
output:
true
false
上面代码中,接口b的动态类型为*int, 而动态值为nil,直接使用等于号无法判断。
所以不能直接通过与nil比较的方式判断动态值是否为空。
可以借助反射来判断。
func IsNil(i interface{}) bool {
defer func() {
recover()
}()
vi := reflect.ValueOf(i)
return vi.IsNil()
}
func (v Value) IsNil() bool
参数v必须是chan, func, interface, map, pointer, or slice,否则会panic。
如果调用IsNil的不是一个指针,会出现异常,需要捕获异常。
func IsNil(i interface{}) bool {
vi := reflect.ValueOf(i)
if vi.Kind() == reflect.Ptr {
return vi.IsNil()
}
return false
}
一个接口包括动态类型和动态值。
如果一个接口的动态类型和动态值都为空,则这个接口为空的。
补充:golang返回值为interface{}的类型判断
看标题就知道,这是一个很简单的问题,就一个函数的事,但是,今天一同学golang的几个人中,已经不止一个人问我了,在这里我就说一下,也希望对不清楚的娃有些许帮助,大神别喷,飘过就行了。
大家知道,golang对于不确定返回值可以用interface{}代替,这确实很方便,但是也带来了问题,那就是如何判断返回值是什么类型的?其实可以用反射也就是reflect来判断,通过函数
reflect.TypeOf()
即返回类型!
感谢你能够认真阅读完这篇文章,希望小编分享的“golang interface如何判断为空nil的实现代码”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。