要判斷interface 空的問題,首先看下其底層實現(xiàn)。
interface 底層結(jié)構(gòu)
根據(jù) interface 是否包含有 method,底層實現(xiàn)上用兩種 struct 來表示:iface 和 eface。eface表示不含 method 的 interface 結(jié)構(gòu),或者叫 empty interface。
對于 Golang 中的大部分數(shù)據(jù)類型都可以抽象出來 _type 結(jié)構(gòu),同時針對不同的類型還會有一些其他信息。
1.eface
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
}
2.iface
iface 表示 non-empty interface 的底層實現(xiàn)。相比于 empty interface,non-empty 要包含一些 method。method 的具體實現(xiàn)存放在 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) 指針和數(shù)據(jù)指針組成,或者說由動態(tài)類型和動態(tài)值組成。
struct Iface
{
Itab* tab;
void* data;
};
struct Itab
{
InterfaceType* inter;
Type* type;
void (*fun[])(void);
};
接口表存儲元數(shù)據(jù)信息,包括接口類型、動態(tài)類型,以及實現(xiàn)接口的方法指針。無論是反射還是通過接口調(diào)用方法,都會用到這些信息。
再來看下nil的定義。
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.
如何判定interface里面的動態(tài)值是否空
對于一個接口的零值就是它的類型和值的部分都是nil。
一個接口值基于它的動態(tài)類型被描述為空或非空。
例如,
一般情況下,通過使用w==nil或者w!=nil來判讀接口值是否為空,只是判斷了動態(tài)類型,而沒有判斷動態(tài)值。
例如,下面的例子。
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的動態(tài)類型為*int, 而動態(tài)值為nil,直接使用等于號無法判斷。
所以不能直接通過與nil比較的方式判斷動態(tài)值是否為空。
那如何判斷動態(tài)值是否為空?
可以借助反射來判斷。
func IsNil(i interface{}) bool {
defer func() {
recover()
}()
vi := reflect.ValueOf(i)
return vi.IsNil()
}
其中,IsNil定義如下:
func (v Value) IsNil() bool
參數(shù)v必須是chan, func, interface, map, pointer, or slice,否則會panic。
如果調(diào)用IsNil的不是一個指針,會出現(xiàn)異常,需要捕獲異常。
或者修改成這樣:
func IsNil(i interface{}) bool {
vi := reflect.ValueOf(i)
if vi.Kind() == reflect.Ptr {
return vi.IsNil()
}
return false
}
總結(jié)
一個接口包括動態(tài)類型和動態(tài)值。
如果一個接口的動態(tài)類型和動態(tài)值都為空,則這個接口為空的。
補充:golang返回值為interface{}的類型判斷
看標(biāo)題就知道,這是一個很簡單的問題,就一個函數(shù)的事,但是,今天一同學(xué)golang的幾個人中,已經(jīng)不止一個人問我了,在這里我就說一下,也希望對不清楚的娃有些許幫助,大神別噴,飄過就行了。
大家知道,golang對于不確定返回值可以用interface{}代替,這確實很方便,但是也帶來了問題,那就是如何判斷返回值是什么類型的?
其實可以用反射也就是reflect來判斷,通過函數(shù)
即返回類型!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。