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

提交的json数据如下:

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

要验证 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")
    } 

 

posted @ 2017-08-29 18:00  码农骆驼  阅读(748)  评论(0编辑  收藏  举报