我们来看一段代码
你觉得14、17、22行的输出分别是什么?
import (
"encoding/json"
"fmt"
)
type Data struct {
One int
two string
three int
}
func main() {
in := Data{1, "two", 3}
fmt.Printf("%#v\n", in)
encoded, _ := json.Marshal(in)
fmt.Println(string(encoded))
var out Data
json.Unmarshal(encoded, &out)
fmt.Printf("%#v\n", out)
}
定义结构体
type Data struct {
One int
two string
three int
}
在结构中
英文大写字母开头的属性是被导出的
而小写字母开头的属性未导出的
因此
One 属性是被导出的
two、three是未导出的
编码
...
in := Data{1, "two", 3}
fmt.Printf("%#v\n", in) //prints main.MyData{One:1, two:"two", three:3}
encoded, _ := json.Marshal(in)
fmt.Println(string(encoded)) //prints {"One":1}
...
在上述2-3行代码&