检查两个切片的相等性 [英] Checking the equality of two slices

查看:85
本文介绍了检查两个切片的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查两个切片是否相等?

How can I check if two slices are equal?

推荐答案

您需要循环切片中的每个元素并进行测试。没有定义切片的平等。但是,如果您正在比较 [] byte 类型的值,则存在 bytes.Equal 函数。

You need to loop over each of the elements in the slice and test. Equality for slices is not defined. However, there is a bytes.Equal function if you are comparing values of type []byte.

func testEq(a, b []Type) bool {

    if a == nil && b == nil { 
        return true; 
    }

    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
}

这篇关于检查两个切片的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆