package main
import (
"encoding/json"
"fmt"
)
func main() {
// 继承
person := person{Id: 22, Name: "kit"}
student := student{person: person, ClassId: 1, ClassName: "236"}
fmt.Print("json前:","\n",student)
// 转json
jsonByte, err := json.Marshal(student)
if err == nil {
fmt.Print("\njson后:","\n",string(jsonByte))
}
}
type person struct{
Id int
Name string
}
type student struct {
person // 继承
ClassId int
ClassName string
}
输出:
D:\develop\go_path\src\go.xushiwei.note>go run test.go
json前:
{{22 kit} 1 236}
json后:
{"Id":22,"Name":"kit","ClassId":1,"ClassName":"236"}