首页 > 解决方案 > 如何检查结构字段值是否存在于另一个结构字段中

问题描述

type Bundle struct {
    Name    string   `bson:"name" json:"name"`
    Choices []string `bson:"choices" json:"choices"`
}

type Event struct {
    ID          bson.ObjectId   `bson:"_id,omitempty" json:"_id,omitempty"` 
    Bundle      []Bundle        `bson:"bundle" json:"bundle"`
}

type AttendeeBundle struct {
    Name    string `bson:"name" json:"name"`
    Details string `bson:"details" json:"details"`
}

type Attendees struct {
    ID          bson.ObjectId `bson:"_id,omitempty" json:"_id,omitempty"`
    Bundle      []AttendeeBundle      `bson:"bundle" json:"bundle"`
}

我正在建立一个活动注册平台。活动包含一个 Bundle 字段,与会者可以从中进行选择。例如。各种尺寸或颜色的 T 恤。

因此,在注册时,我想确保与会者从相应的活动中选择正确的捆绑包。

我知道可以为与会者和活动进行迭代,如下所示:

for _ , attendeebundle := range Attendees.Bundle {      
            for _, eventbundle := range Event.Bundle{
                //comparing attendee bundle and event bundle
            }
        }
    }

但是,我认为这太多余了,还有其他更优雅/有效的方法吗?

标签: godata-structuresstructiteration

解决方案


推荐阅读