2022-10-06:以下go语言代码输出什么?A:[1 2 3] [1 2 3] ;B:[1 2 3] [3 4 5]

2022-10-06:以下 go 语言代码输出什么?A:[1 2 3] [1 2 3] ;B:[1 2 3] [3 4 5]; C:[1 2 3] [3 4 5 6 7 8 9];D:[1 2 3] [3 4 5 0 0 0]。

package main

import (
    "encoding/json"
    "fmt"
)

type AutoGenerated struct {
    Age   int    `json:"age"`
    Name  string `json:"name"`
    Child []int  `json:"child"`
}

func main() {
    jsonStr1 := `{"age": 14,"name": "potter", "child":[1,2,3]}`
    a := AutoGenerated{}
    json.Unmarshal([]byte(jsonStr1), &a)
    aa := a.Child
    fmt.Println(aa)
    jsonStr2 := `{"age": 12,"name": "potter", "child":[3,4,5,7,8,9]}`
    json.Unmarshal([]byte(jsonStr2), &a)
    fmt.Println(aa)
}


















答案选 B。以为选 C,但运行结果感人。根据运行结果推断,第一次序列化 a.Child 是 [1,2,3],第二次序列化的时候,是先修改 a.Child 的值 3,4,5,然后追加 a.Child 的值,而且是一个一个追加的 7,8,9。
在这里插入图片描述

本作品采用《CC 协议》,转载必须注明作者和本文链接
微信公众号:福大大架构师每日一题。最新面试题,涉及 golang,rust,mysql,redis,云原生,算法,分布式,网络,操作系统。
未填写
文章
488
粉丝
23
喜欢
39
收藏
22
排名:440
访问:2.1 万
私信
所有博文
社区赞助商