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

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

對(duì)反射值進(jìn)行解組

對(duì)反射值進(jìn)行解組

Go
qq_花開花謝_0 2021-06-15 17:08:29
package mainimport (    "fmt"    "encoding/json"    "reflect")var (    datajson []byte    //ref mapp)type mapp map[string]reflect.Typetype User struct {    Name string    //Type map[string]reflect.Type}func MustJSONEncode(i interface{}) []byte {    result, err := json.Marshal(i)    if err != nil {        panic(err)    }    return result}func MustJSONDecode(b []byte, i interface{}) {    err := json.Unmarshal(b, i)    if err != nil {        panic(err)    }}func Store(a interface{}) {    datajson = MustJSONEncode(a)    //fmt.Println(datajson)}func Get(a []byte, b interface{}) {    objType := reflect.TypeOf(b).Elem()obj := reflect.New(objType)//fmt.Println(obj)MustJSONDecode(a, &obj)fmt.Printf("%s", obj)    }func main() {    dummy := &User{}    david := User{Name: "DavidMahon"}    Store(david)    Get(datajson, dummy)}在獲取函數(shù)中func Get(a []byte, b interface{}) {    objType := reflect.TypeOf(b).Elem()obj := reflect.New(objType)//fmt.Println(obj)MustJSONDecode(a, &obj)fmt.Printf("%s", obj)    }我無(wú)法將 json 解組為基礎(chǔ)對(duì)象類型。這里有什么問(wèn)題?我被困在這里了。一件很簡(jiǎn)單卻又很難搞清楚的事情。謝謝更新::此問(wèn)題的目標(biāo)是檢索在 Get 函數(shù)中傳遞的類型完整的對(duì)象。Nick 在下面的評(píng)論中提到的方法并沒(méi)有讓我得到我之前已經(jīng)嘗試過(guò)的實(shí)際對(duì)象。我無(wú)論如何都可以在這樣的地圖中檢索數(shù)據(jù)(即使對(duì)象下面有遞歸對(duì)象)func Get(a []byte) {    var f interface{}    //buf := bytes.NewBuffer(a)    //v := buf.String()    //usr := &User{}    MustJSONDecode(a, &f)    fmt.Printf("\n %v \n", f)}但是我需要返回的不僅僅是數(shù)據(jù)的實(shí)際對(duì)象。就像user := &User{"SomeName"}我需要user從 Unmarshall 返回對(duì)象的地方。訣竅是在反射中的某個(gè)地方,但不知道如何。
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(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)