获取两个结构切片/数组在特定字段上的差异(使用Golang)

huangapple go评论97阅读模式
英文:

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

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对Golang还不熟悉,目前在获取两个结构体切片的差异值方面遇到了一些困难。

我有两个结构体切片的数据,我想获取它们之间前三个结构体字段的差异值。所以如果AppointmentType、Date和Time不匹配,它将返回该值。

可以忽略User字段。

我尝试使用这里的解决方案,但我无法配置它以匹配我的结构体。请帮助我解决这个问题!谢谢!

链接:https://stackoverflow.com/questions/19374219/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}]
英文:

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!

https://stackoverflow.com/questions/19374219/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}]

答案1

得分: 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}]

英文:

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}]

答案2

得分: 0

大家好,我根据以下建议找到了解决方案:https://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings

我修改了代码并根据我的需求进行了一些调整。非常感谢大家的帮助!

func getAvailableAppointments() []AppointmentsDetail {
    
	// 将切片放在任何其他位置(因为最终会交换)
	allAppt := getAppointments()
	bookedAppt := getBookedAppointmentsData()

	var diff []AppointmentsDetail

	// 循环两次以进行交换
	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("匹配", s2)
					found = true
					break
				}
			}
			if !found {
				diff = append(diff, s1)
			}
		}
		// 只有在第一次循环时才交换切片
		if i == 0 {
			bookedAppt, allAppt = allAppt, bookedAppt
		}
	}
	fmt.Println("差异:", diff)
	return diff
}
英文:

Hello all I got the solution based on the suggestions https://stackoverflow.com/questions/19374219/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 &lt; 2; i++ {
		for _, s1 := range bookedAppt {
			found := false
			for _, s2 := range allAppt {
				if s1.Date == s2.Date &amp;&amp; s1.Time == s2.Time &amp;&amp; s1.AppointmentType == s2.AppointmentType {
					fmt.Println(&quot;Match&quot;, 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(&quot;Difference:&quot;, diff)
	return diff
}

huangapple
  • 本文由 发表于 2022年12月6日 12:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/74697312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定