Checking the equality of two slices

后端 未结 6 1182
天命终不由人
天命终不由人 2020-12-12 10:05

How can I check if two slices are equal?

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 10:28

    In case that you are interested in writing a test, then github.com/stretchr/testify/assert is your friend.

    Import the library at the very beginning of the file:

    import (
        "github.com/stretchr/testify/assert"
    )
    

    Then inside the test you do:

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

    The error prompted will be:

                    Diff:
                    --- Expected
                    +++ Actual
                    @@ -1,4 +1,4 @@
                     ([]int) (len=2) {
                    + (int) 1,
                      (int) 2,
                    - (int) 2,
                      (int) 1,
    Test:           TestEquality_SomeSlice
    

提交回复
热议问题