2015-01-04 05:00
浏览 778


I want to compare output of sha256.Sum256() which is [32]byte with a []byte.

I am getting an error "mismatched types [32]byte and []byte". I am not able to convert []byte to [32]byte.

Is there a way to do this?

图片转代码服务由CSDN问答提供 功能建议

我想比较sha256.Sum256()的输出,该输出是[32] byte与[] byte。< / p>

我收到一条错误消息:“ [32] byte和[] byte类型不匹配”。 我无法将[] byte转换为[32] byte。


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhuang6177 2015-01-04 06:00

    You can trivially convert any array ([size]T) to a slice ([]T) by, uh, slicing it:

    x := [32]byte{}
    slice := x[:] // shorthand for x[0:len(x)]

    From there you can compare it to your slice like you would compare any other two slices, e.g.

    func Equal(slice1, slice2 []byte) bool {
        if len(slice1) != len(slice2) {
            return false
        for i := range slice1 {
            if slice1[i] != slice2[i] {
                return false
        return true

    Edit: As Dave mentions in the comments, there's also an Equal method in the bytes package, bytes.Equal(x[:], y[:])

    点赞 打赏 评论
  • doumianfeng6979 2015-01-04 05:35

    I got the answer using this thread

    SHA256 in Go and PHP giving different results

        converted := []byte(raw)
        hasher := sha256.New()
        return hex.EncodeToString(hasher.Sum(nil)) == encoded

    This is not converting [32]byte to []byte but it is using different function which do not give output in [32]byte.

    点赞 打赏 评论

相关推荐 更多相似问题