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

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

如何將嵌套結(jié)構(gòu)中的字段設(shè)置為零值?

如何將嵌套結(jié)構(gòu)中的字段設(shè)置為零值?

Go
開心每一天1111 2022-04-26 14:32:01
假設(shè)我有一個(gè)Thing1我想要的 struct 實(shí)例json.Marshaltype Thing1 struct {    A string `json:"a,omitempty"`    B int    `json:"b,omitempty"`    C Thing2 `json:"c,omitempty"`}type Thing2 struct {    D bool `json:"d,omitempty"`    E int  `json:"e,omitempty"`}...thing1 := Thing1{    A: "test",    B: 42,    C: Thing2{D: true, E: 43},}您將如何編寫一個(gè)函數(shù),該函數(shù)采用任何結(jié)構(gòu)的實(shí)例和要編輯的字段列表并返回傳入對(duì)象的克隆(或只是變異),但將已編輯的字段設(shè)置為零值?redact(thing1, []string{"B", "D"})thing1 == Thing1{    A: "test",    B: 0,    C: Thing2{D: false, E: 43},}我不能json:"-"用作字段標(biāo)簽,因?yàn)槲艺谑褂玫牟樵冋Z(yǔ)言(Dgraph)需要當(dāng)前的標(biāo)簽。編輯:不在示例中,但如果適用,還應(yīng)編輯數(shù)組內(nèi)的對(duì)象
查看完整描述

1 回答

?
aluckdog

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

使用反射來(lái)操作結(jié)構(gòu)字段的值。以下是我在評(píng)論中寫的概念證明。由于這只是一個(gè) poc,您可能需要調(diào)整/修改代碼以滿足您的需求。


這個(gè)函數(shù)改變?cè)紨?shù)據(jù)。代碼是不言自明的。


func redact(target interface{}, fieldsToModify []string) {

    // if target is not pointer, then immediately return

    // modifying struct's field requires addresable object

    addrValue := reflect.ValueOf(target)

    if addrValue.Kind() != reflect.Ptr {

        return

    }


    // if target is not struct then immediatelly return

    // this might need to be modified as per your needs

    targetValue := addrValue.Elem()

    targetType := targetValue.Type()

    if targetType.Kind() != reflect.Struct {

        return

    }


    // loop the fields

    for i := 0; i < targetType.NumField(); i++ {

        fType := targetType.Field(i)

        fValue := targetValue.Field(i)


        // if the field type is struct, then call redact() recursively

        if fValue.Kind() == reflect.Struct {

            redact(fValue.Addr().Interface(), fieldsToModify)

            continue

        } 


        // if the field is slice, loop then call redact() recursively

        if fValue.Kind() == reflect.Array || fValue.Kind() == reflect.Slice {

            for i := 0; i < fValue.Len(); i++ {

                redact(fValue.Index(i).Addr().Interface(), fieldsToModify)

            }

            continue

        }


        // loop the fieldsToModify

        for _, fieldToModify := range fieldsToModify {

            if fieldToModify == fType.Name && fValue.CanSet() {

                fValue.Set(reflect.Zero(fType.Type))

            }

        }

    }

}

第一個(gè)參數(shù)中的redact()函數(shù)指針數(shù)據(jù),因?yàn)樾薷淖侄涡枰商砑訉?duì)象。


type Thing2 struct {

    D bool `json:"d,omitempty"`

    E int  `json:"e,omitempty"`

}


type Thing1 struct {

    A string   `json:"a,omitempty"`

    B int      `json:"b,omitempty"`

    C Thing2   `json:"c,omitempty"`

    H []Thing2 `json:"h,omitempty"`

}


thing1 := Thing1{

    A: "test",

    B: 42,

    C: Thing2{D: true, E: 43},

    H: []Thing2{Thing2{D: true, E: 43}},

}


fmt.Printf("before: %#v \n", thing1)

// before: main.Thing1{A:"test", B:42, C:main.Thing2{D:true, E:43}, H:[]main.Thing2{main.Thing2{D:true, E:43}}} 


redact(&thing1, []string{"B", "D"})

fmt.Printf("after: %#v \n", thing1)

// after: main.Thing1{A:"test", B:0, C:main.Thing2{D:false, E:43}, H:[]main.Thing2{main.Thing2{D:false, E:43}}} 

游樂場(chǎng): https: //play.golang.org/p/wy39DGdSVV7


查看完整回答
反對(duì) 回復(fù) 2022-04-26
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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