无法比较字节切片。

huangapple go评论69阅读模式
英文:

Unable to compare byte slices

问题

我正在尝试编写一个单元测试用例,在其中使用reflect.DeepEqual来比较计算得到的结果和期望的结果。结构体中的一个条目是一个字节切片,而DeepEqual一直无法比较它。

示例代码:https://goplay.space/#OcAPkK-EqDX

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var a = []byte("qwedsa")
	var b [6]byte
	copy(b[:], a)
	fmt.Println(reflect.DeepEqual(a, b), len(a), len(b), cap(a), cap(b))
}
英文:

I am trying to write a unit test case, where I'm using reflect.DeepEqual to compare computed and expected results. One of the entries in the struct is a byte slice and DeepEqual keeps on failing it.

Sample Code https://goplay.space/#OcAPkK-EqDX

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var a = []byte("qwedsa")
	var b [6]byte
	copy(b[:], a)
	fmt.Println(reflect.DeepEqual(a, b), len(a), len(b), cap(a), cap(b))
}

答案1

得分: 3

reflect.DeepEqual(a, b) 返回 false,因为你在比较两种不同的类型。

var a = []byte("qwedsa") // 这里 a 是一个长度为 6 的切片

var b [6]byte // 这里 b 是一个长度为 6 的数组

你可以使用以下不同的选项来实现这个目标。

  1. reflect.DeepEqual(a, b[:]) // 通过从数组 b 中获取一个切片来比较

  2. 使用这个方法代替 reflect 包,因为正如 Adrian 在他的评论中提到的,reflect 对性能不好。

bytes.Equal(a, b[:])

  1. 如果不需要将其用作数组,可以直接将 b 创建为与 a 长度相同的切片。
var b = make([]byte, len(a))
bytes.Equal(a, b)
英文:

reflect.DeepEqual(a, b) returns false because you are comparing two types.

var a = []byte("qwedsa") //here a is a slice with length 6

var b [6]byte //here b is a array with length 6

You can use different options to Do this as mentioned in below.

  1. reflect.DeepEqual(a, b[:]) //by getting a slice from b array

  2. use this instead of reflect package because reflect is not good for performance as Adrian mentioned in his comment

bytes.Equal(a, b[:])

  1. create b directly as a slice with length of a if there is no need to use it as an array.
var b = make([]byte, len(a))
bytes.Equal(a, b)

答案2

得分: 2

这是它的翻译:

package main
import "bytes"

func main() {
   var (
      a = []byte("qwedsa")
      b [6]byte
   )
   copy(b[:], a)
   println(bytes.Equal(a, b[:]))
}

https://golang.org/pkg/bytes#Equal

英文:

This does it:

package main
import "bytes"

func main() {
   var (
      a = []byte("qwedsa")
      b [6]byte
   )
   copy(b[:], a)
   println(bytes.Equal(a, b[:]))
}

https://golang.org/pkg/bytes#Equal

答案3

得分: 0

根据您的建议,您需要将byte array转换为byte slice并使用bytes.Equal。以下是相同实现的代码:

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var a = []byte("qwedsa")
	var b [6]byte

	sliceb := b[:]
	copy(sliceb, a)
	fmt.Println(bytes.Equal(a, sliceb))
}

输出结果:

true
英文:

Based on the suggestions you have to convert your byte array to byte slice and use bytes.Equal .Here is the implementation of the same:

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var a = []byte("qwedsa")
	var b [6]byte

	sliceb := b[:]
	copy(sliceb, a)
	fmt.Println(bytes.Equal(a, sliceb))
}

Output:

true

huangapple
  • 本文由 发表于 2021年6月17日 22:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68021179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定