比较数组golang

时间:2014-12-16 17:06:06

标签: arrays go compare

我在Go中定义了自己的类型:

type Sha1Hash [20]byte

我想对这些哈希中的两个进行排序,h1和h2:

func Compare(h1, h2 Sha1Hash) int {

    h1 >= h2 // doens't work, arrays only have == and !=
    bytes.Compare(h1,h2) //doesn't work, Compare only works on slices

}

如何比较我的阵列?

1 个答案:

答案 0 :(得分:5)

您可以从数组中形成切片:

func Compare(h1, h2 Sha1Hash) int {
    return bytes.Compare(h1[0:20], h2[0:20]) 
}