go接口返回空数组
go语言nil返回接口时是null,前端同事要求返回[]空数组,代码片段如下:
res := g.Map{
"list": menus,
"statusOptions": statusOptions,
"typeOptions": typeOptions,
"fields": fields, //nil
}
response.SusJson(true, r, "ok", res)
解决办法:通过go语言自建数组形式替换fields变量
if fields==nil {
fields = make([]int, 0)
}
res := g.Map{
"list": menus,
"statusOptions": statusOptions,
"typeOptions": typeOptions,
"fields": fields,
}
response.SusJson(true, r, "ok", res)
至此解决前端需求,但是感觉代码没有优雅。