特意去掉了 omitempty
tag
为了防止出现
{
"apps": null
}
最推荐的声明方式应该是哪一种?
package main
type App struct {
Id int `json:"id"`
Name string `json:"name"`
}
type Apps []*App
type Domain struct {
Apps Apps
}
const CustomCapacity = 3
func main() {
apps1 := make([]*App, 0, CustomCapacity)
apps2 := make([]App, 0, CustomCapacity)
apps3 := make(Apps, 0, CustomCapacity)
apps4 := Apps{}
apps5 := &Apps{}
}
1
MidGap 2022-01-17 14:20:10 +08:00
type Domain struct {
Apps Apps `json:"apps,omitempty"` } |
2
777777 2022-01-17 17:13:44 +08:00
var domain Domain
app := APP{Id:0, Name:""} domain.Apps := []*APP{&app} // 如果要 3 个 []*APP{&app, &app, &app} 我猜你的需求是因为前端说:空数据也要返回字段。 |
3
eudore 2022-01-18 09:07:50 +08:00
Projects: []*ConfigK8SProject{
{Project: "dev/*", AutoUpdate: true, Cluster: "local2180", Namespace: "dev"}, {Project: "test/esb-*", AutoUpdate: true, Cluster: "local2180", Namespace: "test"}, } |
4
hzzhzzdogee 2022-01-21 22:48:48 +08:00
前三种都不会出现 { "apps": null}
|