golang验证提交的数据中某个字段是否重复

时间:2023-03-09 00:06:30
golang验证提交的数据中某个字段是否重复

提交的json数据如下:

{
"cp_id": ,
"dev_id": ,
"os": ,
"app_id": "",
"ad_type": ,
"slot_id": ,
"screen_mode": "3,2",
"status": ,
"ad_plats":[
{
"ad_plat_id": ,
"template_id": ,
"status":
},
{
"ad_plat_id": 1,
"template_id": ,
"status":
}
]
}

要验证 ad_plats节点下的 ad_plat_id 不能有重复数据,判断代码如下:

    //验证平台数据是否有重复
check := map[string]string{}
for _, plat := range tc.TCAdPlats {
check[strconv.Itoa(int(plat.AdPlatId))] = ""
}
if len(check) != len(tc.TCAdPlats) {
return errors.New("duplicate AdPlat Data")
}