gpt4 book ai didi

arrays - 比较数组 golang

转载 作者:IT王子 更新时间:2023-10-29 01:27:14 27 4
gpt4 key购买 nike

我已经在 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

}

如何比较我的数组?

最佳答案

您可以从数组中形成 slice :

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

关于arrays - 比较数组 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27510036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com