代码来自 chatgpt
package main
import (
"fmt"
"reflect"
)
type Object struct {
ID int
}
func main() {
a := []Object{{1}, {2}, {3}}
b := []Object{{2}, {3}, {4}}
diffAB := diff(a, b)
diffBA := diff(b, a)
fmt.Println("In A not in B: ", diffAB)
fmt.Println("In B not in A: ", diffBA)
}
// diff 函数找出在 s1 中存在,但在 s2 中不存在的元素
func diff(s1, s2 []Object) (diffSlice []Object) {
mapS2 := make(map[int]bool)
for _, item := range s2 {
mapS2[item.ID] = true
}
for _, item := range s1 {
if _, found := mapS2[item.ID]; !found {
diffSlice = append(diffSlice, item)
}
}
return
}
标签:s2,fmt,Object,差集,golang,item,数组,diff
From: https://www.cnblogs.com/hi3254014978/p/17737933.html