To check if two byte slices are equal using the bytes
package in Go, you can use the bytes.Equal()
function. Here's an example:
package main
import (
"bytes"
"fmt"
)
func main() {
slice1 := []byte{1, 2, 3, 4, 5}
slice2 := []byte{1, 2, 3, 4, 5}
slice3 := []byte{5, 4, 3, 2, 1}
if bytes.Equal(slice1, slice2) {
fmt.Println("slice1 and slice2 are equal")
} else {
fmt.Println("slice1 and slice2 are not equal")
}
if bytes.Equal(slice1, slice3) {
fmt.Println("slice1 and slice3 are equal")
} else {
fmt.Println("slice1 and slice3 are not equal")
}
}
In this example, the bytes.Equal()
function is used to compare slice1
and slice2
. Since their elements are the same, the output will be "slice1 and slice2 are equal".
On the other hand, slice1
and slice3
have different element values, so the output will be "slice1 and slice3 are not equal".