golang interface判斷為空nil的實現(xiàn)代碼
要判斷interface 空的問題,首先看下其底層實現(xiàn)。
interface 底層結(jié)構(gòu)
根據(jù) interface 是否包含有 method,底層實現(xiàn)上用兩種 struct 來表示:iface 和 eface。eface表示不含 method 的 interface 結(jié)構(gòu),或者叫 empty interface。
對于 Golang 中的大部分?jǐn)?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 變量怎么存呢?這個下面再細(xì)說。
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)類型被描述為空或非空。
例如,
var w io.Writer
一般情況下,通過使用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)值都為空,則這個接口為空的。
補(bǔ)充:golang返回值為interface{}的類型判斷
看標(biāo)題就知道,這是一個很簡單的問題,就一個函數(shù)的事,但是,今天一同學(xué)golang的幾個人中,已經(jīng)不止一個人問我了,在這里我就說一下,也希望對不清楚的娃有些許幫助,大神別噴,飄過就行了。
大家知道,golang對于不確定返回值可以用interface{}代替,這確實很方便,但是也帶來了問題,那就是如何判斷返回值是什么類型的?
其實可以用反射也就是reflect來判斷,通過函數(shù)
reflect.TypeOf()
即返回類型!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Go中的new()和make()函數(shù)區(qū)別及底層原理詳解
這篇文章主要為大家介紹了Go中的new()和make()函數(shù)區(qū)別及底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Golang Gin框架實現(xiàn)多種數(shù)據(jù)格式返回結(jié)果詳解
這篇文章主要介紹了Golang Gin框架實現(xiàn)多種數(shù)據(jù)格式返回結(jié)果,我們都知道,一個完整的請求包含請求和處理請求以及結(jié)果返回三個步驟,在服務(wù)器端對請求處理完成以后,會將結(jié)果返回給客戶端,在gin框架中,支持返回多種請求數(shù)據(jù)格式,下面我們一起來看看2023-05-05
Go項目與Docker結(jié)合實現(xiàn)高效部署深入探究
在現(xiàn)代軟件開發(fā)中,使用Docker部署應(yīng)用程序已經(jīng)成為一種標(biāo)準(zhǔn)實踐,本文將深入探討如何將Go項目與Docker結(jié)合,實現(xiàn)高效、可靠的部署過程,通過詳細(xì)的步驟和豐富的示例,你將能夠迅速掌握這一流程2023-12-12
Go語言學(xué)習(xí)教程之結(jié)構(gòu)體的示例詳解
結(jié)構(gòu)體是一個序列,包含一些被命名的元素,這些被命名的元素稱為字段(field),每個字段有一個名字和一個類型。本文通過一些示例帶大家深入了解Go語言中結(jié)構(gòu)體的使用,需要的可以參考一下2022-09-09
這些關(guān)于Go中interface{}的注意事項你都了解嗎
這篇文章主要為大家詳細(xì)介紹了學(xué)習(xí)Go語言時需要了解的interface{}注意事項,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03

