go语言学习笔记 - 结构体

    技术2024-01-14  103

    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)) //fmt.Println(string(json2)) }

    输出

    {"name":"zhang san","password":"Aa123456"} {"name":"xiao min","password":"123455","friends":["li si"]} {"A":{"name":"xiao min","password":"123455"},"friends":["wang er ma"]}
    Processed: 0.017, SQL: 9