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

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

為什么 json.Unmarshal 在 golang 中追加一個新指針后會更改指針值?

為什么 json.Unmarshal 在 golang 中追加一個新指針后會更改指針值?

Go
暮色呼如 2022-07-04 10:08:10
我有一個問題,為什么 json.Unmarshal在追加一個新的(test2 )之后首先更改指針值(create_at)(test1)但num在 golang 中沒有改變?json.Unmarshal 會重用地址嗎?我不明白為什么附加一個新值(不要指針值)會影響之前插入的元素,如果我更改 *time.Time -> time.Time,這個問題將得到解決......package mainimport (    "bytes"    "encoding/json"    "fmt"    "time")type test struct {    Num      int        `json:"num"`    CreateAt *time.Time `json:"create_at"`}func main() {    var icr []test    var res test    now := time.Now()    next := time.Now().Add(time.Hour)    test1 := test{        Num:      1,        CreateAt: &now,    }    test2 := test{        Num:      2,        CreateAt: &next,    }    newBytes := new(bytes.Buffer)    json.NewEncoder(newBytes).Encode(test1)    json.Unmarshal(newBytes.Bytes(), &res)    icr = append(icr, res)    fmt.Println(PrettyPrint(icr))    // [    //   {    //     "num": 1,    //     "create_at": "2020-09-24T15:03:00.755169076+08:00"    //   }    // ]    newBytes = new(bytes.Buffer)    json.NewEncoder(newBytes).Encode(test2)    json.Unmarshal(newBytes.Bytes(), &res)    icr = append(icr, res)    fmt.Println(PrettyPrint(icr))    // [    //   {    //     "num": 1,    //     "create_at": "2020-09-24T16:03:00.755169556+08:00"    //   },    //   {    //     "num": 2,    //     "create_at": "2020-09-24T16:03:00.755169556+08:00"    //   }    // ]}// PrettyPrint ...func PrettyPrint(data interface{}) string {    var out bytes.Buffer    b, _ := json.Marshal(data)    json.Indent(&out, b, "", "  ")    return out.String()}
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗 獲得超8個贊

簡短版本:切片中的所有元素都是 的淺拷貝res,因此該CreateAt字段指向相同的值。


詳情:


附加res到時icr,添加到的元素icr是 的副本res。


這適用于該領(lǐng)域Num,可以在res不影響存儲的數(shù)據(jù)的情況下對其進(jìn)行修改icr。這是因為它是基本類型。


但是,CreateAt字段res 是指針,所以副本仍然是同一個指針。的所有元素都icr將CreateAt指向相同的值。對該值的任何修改都將反映在 的所有元素中icr。


你有兩個選擇(至少):


更改CreateAt為 plain time.Time,這意味著它將被復(fù)制而不僅僅是一個指針

第二次使用不同的變量解組。例如:json.Unmarshal(newBytes.Bytes(), &res2)

這是一個沒有 json 或 slice 的更清晰的示例,只有兩個變量,其中一個是另一個的副本:見在 playground 上:


package main


import (

    "bytes"

    "encoding/json"

    "fmt"

    "time"

)


type test struct {

    Num      int        `json:"num"`

    CreateAt *time.Time `json:"create_at"`

}


func main() {

    now := time.Now()

    res := test{1, &now}

    res2 := res


    fmt.Println(PrettyPrint(res), PrettyPrint(res2))

    

    // Modify res2:

    res2.Num = 2

    *res2.CreateAt = time.Now().Add(time.Hour)

    

    fmt.Println(PrettyPrint(res), PrettyPrint(res2))

}


// PrettyPrint ...

func PrettyPrint(data interface{}) string {

    var out bytes.Buffer

    b, _ := json.Marshal(data)

    json.Indent(&out, b, "", "  ")

    return out.String()

}

輸出:


{

  "num": 1,

  "create_at": "2009-11-10T23:00:00Z"

} {

  "num": 1,

  "create_at": "2009-11-10T23:00:00Z"

}

{

  "num": 1,

  "create_at": "2009-11-11T00:00:00Z"

} {

  "num": 2,

  "create_at": "2009-11-11T00:00:00Z"

}

更新res2時,res2.Num不會影響res.Num,因為它是基本類型。但是,res2.CreateAt兩者res.CreateAt都指向同一個對象,因此更改會反映在兩者中。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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