0

I am new to Golang and currently having some difficulty retrieving the difference value of 2 struct slices.

I have 2 slices of structs data and I want to get the difference of the first 3 struct fields between them. So if AppointmentType, Date and Time does not match it will return the value.

The user field can be ignored..

I have tried using the solution here but I am unable to configure it to match my struct.. Please do assist on this..! Thank you!

How to find the difference between two slices of strings

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}]
Tim3
  • 58
  • 8
  • You can concat struct fields as a string and compare them to get different. – Minh Dec 06 '22 at 04:36
  • 1
    Use the code in this [answer](https://stackoverflow.com/a/45428032/5728991) with type `string` replaced by `AppointmentsDetail`. – Charlie Tumahai Dec 06 '22 at 04:40
  • 1
    What problem did you run into trying to implement the linked question/answers? – Hymns For Disco Dec 06 '22 at 04:40
  • @CeriseLimón Hello sorry, I updated my question.. I am trying to get the differences of the first 3 fields of the structs that matches instead of the entire data.. – Tim3 Dec 06 '22 at 04:45
  • @HymnsForDisco I just updated my question sorry for the confusion.. – Tim3 Dec 06 '22 at 04:52
  • 1
    Using [ANisus's answer](https://stackoverflow.com/a/19374861/11424673) should work if you change the equality check `if s1 == s2` to something you prefer. – Hymns For Disco Dec 06 '22 at 04:56

2 Answers2

1

Use embedded struct. Hope this helps.

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
}

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

chipsy
  • 65
  • 6
  • Thank you for your answer! This works too for an embedded struct but I needed to work with my given struct itself. – Tim3 Dec 06 '22 at 09:15
0

Hello all I got the solution based on the suggestions How to find the difference between two slices of strings

I modified the code and tweak it a little to my needs. Thank you all for your help!

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
}
Tim3
  • 58
  • 8