To compare two byte slices for case-insensitive equality in Go using the bytes
package, you can make use of the bytes.EqualFold
function. This function performs a case-insensitive byte-by-byte comparison between two byte slices and returns true
if they have the same content, regardless of the case.
Here's an example code snippet demonstrating the usage of bytes.EqualFold
:
package main
import (
"bytes"
"fmt"
)
func main() {
// Sample byte slices
slice1 := []byte("Hello")
slice2 := []byte("hello")
// Comparing byte slices for case-insensitive equality
equal := bytes.EqualFold(slice1, slice2)
// Output the result
fmt.Println(equal) // Output: true
}
In the above example, bytes.EqualFold
is used to compare two byte slices slice1
and slice2
for case-insensitive equality. Since the content of both slices is the same despite different case, it returns true
.
Note: The bytes.EqualFold
function is available starting from Go version 1.5. If you are using an older version, you can convert the byte slices to lowercase and then compare them using bytes.Equal
.