第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 go 反射包中,調(diào)用 Value.Kind() 是 Value.Type().Kind()

在 go 反射包中,調(diào)用 Value.Kind() 是 Value.Type().Kind()

Go
寶慕林4294392 2021-07-07 18:01:15
這兩個(gè)的reflect.Type接口和reflect.Value類型實(shí)現(xiàn)了相同的Kind()方法簽名,假設(shè)我們有一定的價(jià)值目標(biāo)v := reflect.ValueOf(x)是v.Kind()只需要調(diào)用v.Type().Kind()?
查看完整描述

2 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

它們包含相同的值,但似乎不是指同一件事:


type.go源碼

value.go源碼

AType通常由未導(dǎo)出的結(jié)構(gòu)rtype(via TypeOf)實(shí)現(xiàn),而Value包含 a*rtype和 extends flag,它本身是 的簡化形式Kind:


// flag holds metadata about the value.

// The lowest bits are flag bits:

//  - flagRO: obtained via unexported field, so read-only

//  - flagIndir: val holds a pointer to the data

//  - flagAddr: v.CanAddr is true (implies flagIndir)

//  - flagMethod: v is a method value.

// The next five bits give the Kind of the value.

// This repeats typ.Kind() except for method values.

// The remaining 23+ bits give a method number for method values.

// If flag.kind() != Func, code can assume that flagMethod is unset.

// If typ.size > ptrSize, code can assume that flagIndir is set.

拿到ValueOf東西時(shí):


// ValueOf returns a new Value initialized to the concrete value

// stored in the interface i.  ValueOf(nil) returns the zero Value.

func ValueOf(i interface{}) Value {

    [...]

    // For an interface value with the noAddr bit set,

    // the representation is identical to an empty interface.

    eface := *(*emptyInterface)(unsafe.Pointer(&i))

    typ := eface.typ



    /** Flag is built from the type, then kept separate (my comment) */


    fl := flag(typ.Kind()) << flagKindShift

    if typ.size > ptrSize {

        fl |= flagIndir

    }

    return Value{typ, unsafe.Pointer(eface.word), fl}

}

所以當(dāng)你得到一個(gè) Value 類型時(shí)(記住它擴(kuò)展了它的標(biāo)志):


func (v Value) Kind() Kind {

    return v.kind()

}


func (f flag) kind() Kind {

    return Kind((f >> flagKindShift) & flagKindMask)

}

同時(shí)獲取一種類型:(Type是一個(gè)接口,通常由 實(shí)現(xiàn)*rtype)


func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }

因此,盡管他們似乎是在大多數(shù)的情況下平等,v.Kind()是不 v.Type().Kind()


查看完整回答
反對(duì) 回復(fù) 2021-07-12
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

文件reflect/value.go聲明reflect.Value“重復(fù)typ.Kind() 除了方法值”的實(shí)現(xiàn)中的相關(guān)字段。所以,除非值是一個(gè)方法,value.Kind()并且value.Type().Kind()返回相同的數(shù)字。


查看完整回答
反對(duì) 回復(fù) 2021-07-12
  • 2 回答
  • 0 關(guān)注
  • 454 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)