我有两个列表: a []string
, b []string
。 b
列表包含了与a
列表中所有元素相同的元素,以及一个额外的元素。 在两个列表中,索引: 值不匹配,但值相同,否则我可以使用 strings.Join()
和 strings.Replace()
进行操作。这是我的尝试:
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
func compare(start_keys []string, end_keys []string) string {
match := make([]string, len(start_keys))
q := 0
for i := 0; i < len(end_keys); i++ {
for z := 0; z < len(start_keys); z++ {
if end_keys[i] == start_keys[z] {
match[q] = start_keys[z]
q++
}
}
} // now matches known
for i := 0; i < len(end_keys); i++ {
n := end_keys[i] // current element to compare to
s := nip(match, n) // compare result
if s == true { // if no matches, print
a := end_keys[i]
fmt.Println("Not present: ", a)
}
i++
}
fmt.Println("List=", match)
return "error"
}
func nip(matches []string, element string) bool {
zyx := 0
for n := 0; n < len(matches); n++ {
if matches[n] == element {
n++
zyx++
} else if matches[n] != element {
n++
}
}
if zyx == 0 {
return true
}
return false
}
func main() {
start_keys := []string{"a", "b", "c", "d", "e", "f", "g"}
end_keys := []string{"a", "1sdsdfsdfsdsdf", "c", "d", "e", "f", "g", "b"}
x := compare(start_keys, end_keys)
x = x
foo := cmp.Diff(start_keys, end_keys)
fmt.Println(foo)
}
matches[]
保存了我期望的内容,但是nip()
在某些匹配项上返回true,而对于唯一值1sdsdfsdfsdsdf
则不返回true。
a
a :::: a
c
a :::: c
d :::: c
f :::: c
b :::: c
Not present: c
e
a :::: e
d :::: e
f :::: e
b :::: e
Not present: e
g
a :::: g
d :::: g
f :::: g
b :::: g