zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Go输出语言代码 什么 2022 10 以下
2023-06-13 09:13:50 时间

2022-10-06:以下go语言代码输出什么?A:1 2 3 ;B:1 2 3; C:1 2 3;D:1 2 3。

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。

在这里插入图片描述