首頁  >  文章  >  後端開發  >  判斷struct的interface-type欄位是否設定

判斷struct的interface-type欄位是否設定

PHPz
PHPz轉載
2024-02-09 18:27:08656瀏覽

判斷struct的interface-type欄位是否設定

php小編小新在這裡分享一個關於判斷struct的interface-type欄位是否設定的技巧。在Go語言中,struct類型可以實作多個接口,透過判斷interface-type欄位是否設置,我們可以輕鬆判斷一個struct是否實作了某個特定的接口。這個技巧非常實用,可以在程式碼中準確判斷物件的類型,以便進行相對應的處理。接下來,我們一起來看看具體的實作方法吧!

問題內容

給定一個結構體,其欄位屬於介面類型:

type a interface { foo() }
type b interface { bar() }

type container struct {
   fielda a
   fieldb b
   ...
}

以及實作這些介面的結構:

type a struct {}
func (*a) foo() {}
func newa() *a { return &a{} }

type b struct {}
func (*b) bar() {}
func newb() *b { return &b{} }

以及建立 container 實例但未明確設定所有欄位的程式碼:

c := &container{}
c.fielda = newa()
// c.fieldb = newb()  // <-- fieldb not explicitly set

使用反射,如何偵測未明確設定的欄位?

在執行時檢查 fieldb 的值,vscode 愉快地報告該值為 nil。同樣,嘗試呼叫 c.fieldb.bar() 將會因 nil 指標取消引用而發生恐慌。但反射不允許您測試 isnil,iszero 也不會回傳 true:

func validateContainer(c *container) {
    tC := reflect.TypeOf(*c)
    for i := 0; i < tC.NumField(); i++ {
        reflect.ValueOf(tC.Field(i)).IsNil() // panics
        reflect.ValueOf(tC.Field(i)).IsZero() // always false
        reflect.ValueOf(tC.Field(i)).IsValid() // always true
    }
}

解決方法

您應該檢查 reflect.valueof(*c) 的字段,而不是 reflect.typeof(*c)

package main

import (
    "fmt"
    "reflect"
)

func validatecontainer(c *container) {
    tc := reflect.typeof(*c)
    vc := reflect.valueof(*c)
    for i := 0; i < vc.numfield(); i++ {
        if f := vc.field(i); f.kind() == reflect.interface {
            fmt.printf("%v: isnil: %v, iszero: %v, isvalid: %v\n",
                tc.field(i).name,
                f.isnil(),
                f.iszero(),
                f.isvalid(),
            )
        }
    }

    // tc.field(i) returns a reflect.structfield that describes the field.
    // it's not the field itself.
    fmt.printf("%#v\n", reflect.valueof(tc.field(0)))
}

type a interface{ foo() }
type b interface{ bar() }

type container struct {
    fielda a
    fieldb b
}

type a struct{}

func (*a) foo() {}
func newa() *a  { return &a{} }

func main() {
    c := &container{}
    c.fielda = newa()
    validatecontainer(c)
}

輸出:

FieldA: IsNil: false, isZero: false, IsValid: true
FieldB: IsNil: true, isZero: true, IsValid: true
reflect.StructField{Name:"FieldA", PkgPath:"", Type:(*reflect.rtype)(0x48de20), Tag:"", Offset:0x0, Index:[]int{0}, Anonymous:false}

以上是判斷struct的interface-type欄位是否設定的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除