Compare golang arrays

I defined my own type in Go:

type Sha1Hash [20]byte

I would like to sort two of these hashes, h1 and 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

}

How can I compare my arrays?

+4
source share
1 answer

You can form a slice from an array:

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

All Articles