Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 比较两个切片以查找缺少的元素_Arrays_Go - Fatal编程技术网

Arrays 比较两个切片以查找缺少的元素

Arrays 比较两个切片以查找缺少的元素,arrays,go,Arrays,Go,我有两部分:a[]string,b[]stringb包含与a相同的所有元素,外加一个额外的元素。索引:值彼此不匹配,但值匹配,否则我可以执行类似于strings.Join()和strings.Replace()的操作。这是我正在尝试的 package main import ( "fmt" "github.com/google/go-cmp/cmp" ) func compare(start_keys []string, end_keys []string) string {

我有两部分:
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)
}
比较两个切片以查找缺少的元素

我有两部分:
a[]string
b[]string
<代码>b包含所有相同的 元素为
a
加上额外的元素

比如说,

package main

import (
    "fmt"
)

func missing(a, b []string) string {
    ma := make(map[string]bool, len(a))
    for _, ka := range a {
        ma[ka] = true
    }
    for _, kb := range b {
        if !ma[kb] {
            return kb
        }
    }
    return ""
}

func main() {
    a := []string{"a", "b", "c", "d", "e", "f", "g"}
    b := []string{"a", "1sdsdfsdfsdsdf", "c", "d", "e", "f", "g", "b"}
    fmt.Println(missing(a, b))
}
输出:

1sdsdfsdfsdsdf
输出

a and b diffs []
a and c diffs [x y z]

我添加了一些打印来可视化正在发生的事情,现在它更有意义了。谢谢你的解决方案。
/*
An example of how to find the difference between two slices.
This example uses empty struct (0 bytes) for map values.
*/

package main

import (
    "fmt"
)

// empty struct (0 bytes)
type void struct{}

// missing compares two slices and returns slice of differences
func missing(a, b []string) []string {
    // create map with length of the 'a' slice
    ma := make(map[string]void, len(a))
    diffs := []string{}
    // Convert first slice to map with empty struct (0 bytes)
    for _, ka := range a {
        ma[ka] = void{}
    }
    // find missing values in a
    for _, kb := range b {
        if _, ok := ma[kb]; !ok {
            diffs = append(diffs, kb)
        }
    }
    return diffs
}

func main() {
    a := []string{"a", "b", "c", "d", "e", "f", "g"}
    b := []string{"a", "c", "d", "e", "f", "g", "b"}
    c := []string{"a", "b", "x", "y", "z"}
    fmt.Println("a and b diffs", missing(a, b))
    fmt.Println("a and c diffs", missing(a, c))
}
a and b diffs []
a and c diffs [x y z]