根据某些字段获取两个结构体切片/数组之间的差异 (Golang) [duplicate]

Get difference between Two Struct Slices/Array based on certain fields (Golang) [duplicate]

提问人:Stephie B.Natalia 提问时间:12/6/2022 最后编辑:Stephie B.Natalia 更新时间:12/6/2022 访问量:769

问:

我是 Golang 的新手,目前在检索 2 个结构切片的差值时遇到了一些困难。

我有 2 个结构数据切片,我想获得它们之间前 3 个结构字段的差异。因此,如果 AppointmentType、Date 和 Time 不匹配,它将返回该值。

可以忽略用户字段。

我尝试在这里使用该解决方案,但无法将其配置为与我的结构相匹配。请协助..!谢谢!

如何找到两片字符串之间的区别

type AppointmentsDetail struct {
    AppointmentType string 
    Date            string 
    Time            string 
    User            string
}

Slice 1: [{Consult 01-12-2022 15:00 Nil} {Surgery 02-12-2022 12:00 Nil} {Surgery 01-01-2022 12:00 Nil} {Surgery 11-11-2023 12:00 Nil}]

Slice 2: [{Consult 01-12-2022 15:00 Admin} {Surgery 02-12-2022 12:00 Admin}]

getDifference(slice1, slice2)
Output: [{Surgery 01-01-2022 12:00 Admin} {Surgery 11-11-2023 12:00 Admin}]
数组 go struct slice

评论

0赞 Minh 12/6/2022
您可以将结构字段组合成一个字符串并比较它们以获得不同的结果。
1赞 Cerise Limón 12/6/2022
使用此答案中的代码,将类型替换为 。stringAppointmentsDetail
1赞 Hymns For Disco 12/6/2022
您在尝试实现链接的问题/答案时遇到了什么问题?
0赞 Stephie B.Natalia 12/6/2022
@CeriseLimón 你好,对不起,我更新了我的问题..我正在尝试获取匹配的结构的前 3 个字段的差异,而不是整个数据。
1赞 Hymns For Disco 12/6/2022
如果您将相等性检查更改为您喜欢的选项,则使用 ANisus 的答案应该有效。if s1 == s2

答:

1赞 chipsy 12/6/2022 #1

使用嵌入式结构体。希望这会有所帮助。

type AppointmentsDetail struct {
    Data
    User string
}

type Data struct{ AppointmentType, Date, Time string }

func main() {
    var p1 = []AppointmentsDetail{
        {Data{"Consult", "01-12-2022", "15:00"}, "Nil"}, {Data{"Surgery", "02-12-2022", "12:00"}, "Nil"},
        {Data{"Surgery", "01-01-2022", "12:00"}, "Nil"}, {Data{"Surgery", "11-11-2023", "12:00"}, "Nil"},
    }
    var p2 = []AppointmentsDetail{
        {Data{"Consult", "01-12-2022", "15:00"}, "Admin"},
        {Data{"Surgery", "02-12-2022", "12:00"}, "Admin"},
    }

    fmt.Println(getDifference(p1, p2))
}

func getDifference(a, b []AppointmentsDetail) []AppointmentsDetail {
    mb := make(map[Data]struct{}, len(b))

    for _, x := range b {
        mb[x.Data] = struct{}{}
    }

    var diff []AppointmentsDetail
    for _, x := range a {

        if _, found := mb[x.Data]; !found {
            diff = append(diff, x)
        }
    }
    return diff
}

输出:[{{Surgery 01-01-2022 12:00} Nil} {{Surgery 11-11-2023 12:00} Nil}]

评论

0赞 Stephie B.Natalia 12/6/2022
谢谢你的回答!这也适用于嵌入式结构,但我需要使用给定的结构本身。
0赞 Stephie B.Natalia 12/6/2022 #2

大家好,我根据建议得到了解决方案:如何找到两个字符串切片之间的差异

我修改了代码并根据我的需要对其进行了一些调整。谢谢大家的帮助!

func getAvailableAppointments() []AppointmentsDetail {
    
// Slices can be placed in any other (as it will be swapped eventually)
    allAppt := getAppointments()
    bookedAppt := getBookedAppointmentsData()

    var diff []AppointmentsDetail

    //Loop twice to swap
    for i := 0; i < 2; i++ {
        for _, s1 := range bookedAppt {
            found := false
            for _, s2 := range allAppt {
                if s1.Date == s2.Date && s1.Time == s2.Time && s1.AppointmentType == s2.AppointmentType {
                    fmt.Println("Match", s2)
                    found = true
                    break
                }
            }
            if !found {
                diff = append(diff, s1)
            }
        }
        // Swap the slices, only if it was the first loop
        if i == 0 {
            bookedAppt, allAppt = allAppt, bookedAppt
        }
    }
    fmt.Println("Difference:", diff)
    return diff
}