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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何向切片反射添加元素?

如何向切片反射添加元素?

Go
天涯盡頭無女友 2022-01-17 19:59:58
我正在學習Go,我正在做一些反思。我陷入了這樣的情況:我想創(chuàng)建一個slice傳遞struct給函數的interface{}然后我想將新元素附加到這個切片這是一個帶有代碼示例的游樂場。package mainimport (    "fmt"    "reflect")type A struct{ Name string }func main() {    bbb(A{})}func aaa(v interface{}) {    sl := reflect.ValueOf(v).Elem()    typeOfT := sl.Type()    ptr := reflect.New(typeOfT).Interface()    s := reflect.ValueOf(ptr).Elem()    sl.Set(reflect.Append(sl, s))    ptr = reflect.New(typeOfT).Interface()    s = reflect.ValueOf(ptr).Elem()    sl.Set(reflect.Append(sl, s))}func bbb(v interface{}) {    myType := reflect.TypeOf(v)    models := reflect.Zero(reflect.SliceOf(myType)).Interface()    aaa(&models)    fmt.Println(models)}錯誤: panic: reflect: call of reflect.Append on interface Value有沒有辦法讓它工作?注意:我想參考。解決方案:這是我設法做的:操場。
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

問題出reflect.Zero在我創(chuàng)建 slie 的reflect.New時候。在這里,您有完整的工作示例。


https://play.golang.org/p/3mIEFqMxk-


package main


import (

    "encoding/json"

    "fmt"

    "reflect"

)


type A struct {

    Name string `column:"email"`

}


func main() {

    bbb(&A{})

}


func aaa(v interface{}) {

    t := reflect.TypeOf(v)

    if t.Kind() == reflect.Ptr {

        t = t.Elem()

    }


    if t.Kind() == reflect.Slice {

        t = t.Elem()

    } else {

        panic("Input param is not a slice")

    }


    sl := reflect.ValueOf(v)


    if t.Kind() == reflect.Ptr {

        sl = sl.Elem()

    }


    st := sl.Type()

    fmt.Printf("Slice Type %s:\n", st)


    sliceType := st.Elem()

    if sliceType.Kind() == reflect.Ptr {

        sliceType = sliceType.Elem()

    }

    fmt.Printf("Slice Elem Type %v:\n", sliceType)


    for i := 0; i < 5; i++ {

        newitem := reflect.New(sliceType)

        newitem.Elem().FieldByName("Name").SetString(fmt.Sprintf("Grzes %d", i))


        s := newitem.Elem()

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

            col := s.Type().Field(i).Tag.Get("column")

            fmt.Println(col, s.Field(i).Addr().Interface())

        }


        sl.Set(reflect.Append(sl, newitem))

    }

}


func bbb(v interface{}) {

    t := reflect.TypeOf(v)

    if t.Kind() == reflect.Ptr {

        t = t.Elem()

    }

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

        panic("Input param is not a struct")

    }


    models := reflect.New(reflect.SliceOf(reflect.TypeOf(v))).Interface()


    aaa(models)


    fmt.Println(models)


    b, err := json.Marshal(models)

    if err != nil {

        panic(err)

    }

    fmt.Println(string(b))

}



查看完整回答
反對 回復 2022-01-17
?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

這是做你想做的嗎?(游樂場鏈接)


package main


import (

    "fmt"

    "reflect"

)


type A struct{ Name string }


func main() {

    bbb(A{})

}


func aaa(v interface{}) interface{} {

    sl := reflect.Indirect(reflect.ValueOf(v))

    typeOfT := sl.Type().Elem()


    ptr := reflect.New(typeOfT).Interface()

    s := reflect.ValueOf(ptr).Elem()

    return reflect.Append(sl, s)

}


func bbb(v interface{}) {

    myType := reflect.TypeOf(v)

    models := reflect.MakeSlice(reflect.SliceOf(myType), 0, 1).Interface()

    fmt.Println(aaa(models))

}

請注意,我正在返回新切片。我認為您需要另一層間接(指向指針的指針)才能在沒有返回值的情況下執(zhí)行此操作,但這是我第一次在 Go 中進行反射,所以我可能會做錯事。


查看完整回答
反對 回復 2022-01-17
  • 2 回答
  • 0 關注
  • 245 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號