关于go:检查两个切片的相等性

Checking the equality of two slices

如何检查两片是否相等?


您应该使用reflect.deepequal()。

DeepEqual is a recursive relaxation of Go's == operator.

DeepEqual reports whether x and y are"deeply equal," defined as
follows. Two values of identical type are deeply equal if one of the
following cases applies. Values of distinct types are never deeply
equal.

Array values are deeply equal when their corresponding elements are
deeply equal.

Struct values are deeply equal if their corresponding fields, both
exported and unexported, are deeply equal.

Func values are deeply equal if both are nil; otherwise they are not
deeply equal.

Interface values are deeply equal if they hold deeply equal concrete
values.

Map values are deeply equal if they are the same map object or if they
have the same length and their corresponding keys (matched using Go
equality) map to deeply equal values.

Pointer values are deeply equal if they are equal using Go's ==
operator or if they point to deeply equal values.

Slice values are deeply equal when all of the following are true: they
are both nil or both non-nil, they have the same length, and either
they point to the same initial entry of the same underlying array
(that is, &x[0] == &y[0]) or their corresponding elements (up to
length) are deeply equal. Note that a non-nil empty slice and a nil
slice (for example, []byte{} and []byte(nil)) are not deeply equal.

Other values - numbers, bools, strings, and channels - are deeply
equal if they are equal using Go's == operator.


您需要循环切片和测试中的每个元素。未定义切片的相等性。但是,如果要比较类型[]byte的值,则有一个bytes.Equal函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func testEq(a, b []Type) bool {

    // If one is nil, the other must also be nil.
    if (a == nil) != (b == nil) {
        return false;
    }

    if len(a) != len(b) {
        return false
    }

    for i := range a {
        if a[i] != b[i] {
            return false
        }
    }

    return true
}


这只是使用@victorderyagin答案中给出的reflect.deepequal()的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
   "fmt"
   "reflect"
)

func main() {
    a := []int {4,5,6}
    b := []int {4,5,6}
    c := []int {4,5,6,7}

    fmt.Println(reflect.DeepEqual(a, b))
    fmt.Println(reflect.DeepEqual(a, c))

}

结果:

1
2
true
false

如果有两个[]byte,请使用bytes.equal比较它们。Golang文件上说:

Equal returns a boolean reporting whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
   "fmt"
   "bytes"
)

func main() {
    a := []byte {1,2,3}
    b := []byte {1,2,3}
    c := []byte {1,2,2}

    fmt.Println(bytes.Equal(a, b))
    fmt.Println(bytes.Equal(a, c))
}

这将打印

1
2
true
false

如果你有兴趣写一个测试,那么github.com/stretchr/testify/assert就是你的朋友。

在文件开头导入库:

1
2
3
import (
   "github.com/stretchr/testify/assert"
)

然后在测试中,你会:

1
2
3
4
5
func TestEquality_SomeSlice (t * testing.T) {
    a := []int{1, 2}
    b := []int{2, 1}
    assert.Equal(t, a, b)
}

提示的错误为:

1
2
3
4
5
6
7
8
9
10
                Diff:
                --- Expected
                +++ Actual
                @@ -1,4 +1,4 @@
                 ([]int) (len=2) {
                + (int) 1,
                  (int) 2,
                - (int) 2,
                  (int) 1,
Test:           TestEquality_SomeSlice