go有没有类似php isset函数
1. 运行环境
当前使用的Go
版本1.18
2. 问题描述?
客户端传过来的json,我通过struct进行解析,但是存在几个问题,如果struct下面包含slice下面在包含个slice,我如何判断最里层的某个值是否存在例子
type Data struct {
Imp []struct {
Id string `json:"id"`
Slotid string `json:"slotid"`
Bid []struct {
Bidtype int `json:"bidtype"`
Bidfloor float64 `json:"bidfloor"`
} `json:"bidinfo"`
Native map[string]interface{} `json:"native"`
} `json:"imp"`
}
这时候我怎么判断data.imp[0].bid[0].bidfloor是否存在,存在的情况有imp可能是nil或者bid是nil
目前我的做法是len(data.imp) >0 and len(data.imp[0].bidi) >0 然后在进行处理,但是总感觉不是很友好
推荐文章: