Checking the equality of two slices

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

How can I check if two slices are equal?

6条回答
  •  隐瞒了意图╮
    2020-12-12 10:43

    This is just example using reflect.DeepEqual() that is given in @VictorDeryagin's answer.

    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))
    
    }
    

    Result:

    true
    false
    

    Try it in Go Playground

提交回复
热议问题