1 回答

TA貢獻1865條經驗 獲得超7個贊
將每個結構聲明為命名類型將避免重復重寫整個結構類型:
type Q struct {
Links Links `json:"_links"`
CreatedAt time.Time `json:"created_at"`
ID string `json:"id"`
Name string `json:"name"`
UpdatedAt time.Time `json:"updated_at"`
}
type Links struct {
Self Self `json:"self"`
}
type Self struct {
Href string `json:"href"`
}
func main() {
expected, _ := json.Marshal(
Q{Links: Links{
Self: Self{
Href: "testurl",
},
},
ID: "testid",
Name: "testname",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
fmt.Println(string(expected))
}
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報