婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > golang interface判斷為空nil的實現代碼

golang interface判斷為空nil的實現代碼

熱門標簽:壽光微信地圖標注 評價高的400電話辦理 外呼系統用什么卡 電話機器人軟件免費 涿州代理外呼系統 百度地圖標注后傳給手機 阿克蘇地圖標注 外呼系統顯本地手機號 excel地圖標注分布數據

要判斷interface 空的問題,首先看下其底層實現。

interface 底層結構

根據 interface 是否包含有 method,底層實現上用兩種 struct 來表示:iface 和 eface。eface表示不含 method 的 interface 結構,或者叫 empty interface。

對于 Golang 中的大部分數據類型都可以抽象出來 _type 結構,同時針對不同的類型還會有一些其他信息。

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 的底層實現。相比于 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的定義

// 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里面的動態值是否空

對于一個接口的零值就是它的類型和值的部分都是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()
}

其中,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()

即返回類型!

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Go語言中nil判斷引起的問題詳析

標簽:重慶 吐魯番 汕頭 蘭州 雞西 梅河口 銅川 欽州

巨人網絡通訊聲明:本文標題《golang interface判斷為空nil的實現代碼》,本文關鍵詞  golang,interface,判斷,為,空,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《golang interface判斷為空nil的實現代碼》相關的同類信息!
  • 本頁收集關于golang interface判斷為空nil的實現代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 德昌县| 玉溪市| 桐柏县| 孝感市| 遂溪县| 济南市| 卢氏县| 成安县| 防城港市| 武宁县| 驻马店市| 班玛县| 南乐县| 若尔盖县| 怀集县| 高清| 美姑县| 聂荣县| 保德县| 三门县| 福泉市| 兖州市| 满洲里市| 丰城市| 瑞安市| 固始县| 龙游县| 古浪县| 长子县| 玉环县| 理塘县| 南投市| 察隅县| 常山县| 河北省| 丰镇市| 安义县| 清新县| 日土县| 富民县| 沽源县|