英文:
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!
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 < 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论