type A
struct {
Name
string `json:"name"`
Password
string `json:"password"`
Email
string `json:"email,omitempty"`
}
type B
struct {
*A
Friends
[] string `json:"friends,omitempty"`
}
type C
struct {
A
*A
Friends
[] string `json:"friends,omitempty"`
}
func main() {
a
:= &A
{Name
:"xiao min", Password
:"123455"}
j1
, _:=json
.Marshal(A
{
Name
: "zhang san",
Password
: "Aa123456",
})
fmt
.Println(string(j1
))
j2
, _:=json
.Marshal(&B
{A
:a
, Friends
:[]string{"li si"}})
fmt
.Println(string(j2
))
j3
, _:=json
.Marshal(&C
{A
:a
, Friends
:[]string{"wang er ma"}})
fmt
.Println(string(j3
))
}
输出
{"name":"zhang san","password":"Aa123456"}
{"name":"xiao min","password":"123455","friends":["li si"]}
{"A":{"name":"xiao min","password":"123455"},"friends":["wang er ma"]}