×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang bytes.EqualFold() Function with Examples

Golang | bytes.EqualFold() Function: Here, we are going to learn about the EqualFold() function of the bytes package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 22, 2021

bytes.EqualFold()

The EqualFold() function is an inbuilt function of the bytes package which is used to check whether the given byte slices s and t (interpreted as UTF-8 strings) are equal under Unicode case-folding, which is a more general form of case-insensitivity.

It accepts two parameters (s, t []byte) and returns true if b and t are equal under Unicode case-folding; false, otherwise.

Syntax

func EqualFold(s, t []byte) bool

Parameters

  • s, t : The byte slices to be checked.

Return Value

The return type of the bytes.EqualFold() function is a bool, it returns true if b and t are equal under Unicode case-folding; false, otherwise.

Example 1

// Golang program to demonstrate the
// example of bytes.EqualFold() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Declaring byte slices
	var a = []byte{10, 20, 30, 70, 120, 255}
	var b = []byte{100, 120, 50, 70, 120, 255}
	var c = []byte{10, 20, 30, 70, 120, 255}

	// Comparing byte slices and printing results
	fmt.Println(bytes.EqualFold(a, b))
	fmt.Println(bytes.EqualFold(b, c))
	fmt.Println(bytes.EqualFold(a, c))
}

Output:

false
false
true

Example 2

// Golang program to demonstrate the
// example of bytes.EqualFold() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Declaring byte slices
	var a = []byte{10, 20, 30, 70, 120, 255}
	var b = []byte{100, 120, 50, 70, 120, 255}
	var c = []byte{10, 20, 30, 70, 120, 255}

	// Comparing byte slices and printing results
	result := bytes.EqualFold(a, b)
	if result == true {
		fmt.Printf("%t, both a and b are Equal\n", result)
	} else {
		fmt.Printf("%t, both a and b are not Equal\n", result)
	}

	result = bytes.EqualFold(b, c)
	if result == true {
		fmt.Printf("%t, both b and c are Equal\n", result)
	} else {
		fmt.Printf("%t, both b and c are not Equal\n", result)
	}

	result = bytes.EqualFold(a, c)
	if result == true {
		fmt.Printf("%t, both a and c are Equal\n", result)
	} else {
		fmt.Printf("%t, both a and c are not Equal\n", result)
	}
}

Output:

false, both a and b are not Equal
false, both b and c are not Equal
true, both a and c are Equal

Example 3:

// Golang program to demonstrate the
// example of bytes.EqualFold() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Comparing strings
	var a string
	var b string

	a = "Hello, world!"
	b = "Hello, world!"

	if bytes.EqualFold([]byte(a), []byte(b)) == true {
		fmt.Printf("%q and %q are equal under Unicode case-folding\n", a, b)
	} else {
		fmt.Printf("%q and %q are not equal under Unicode case-folding\n", a, b)
	}

	a = "Hello, world!"
	b = "Hello, WORLD!"

	if bytes.EqualFold([]byte(a), []byte(b)) == true {
		fmt.Printf("%q and %q are equal under Unicode case-folding\n", a, b)
	} else {
		fmt.Printf("%q and %q are not equal under Unicode case-folding\n", a, b)
	}

	a = "Hello, world!"
	b = "Hi, world!"

	if bytes.EqualFold([]byte(a), []byte(b)) == true {
		fmt.Printf("%q and %q are equal under Unicode case-folding\n", a, b)
	} else {
		fmt.Printf("%q and %q are not equal under Unicode case-folding\n", a, b)
	}
}

Output:

"Hello, world!" and "Hello, world!" are equal under Unicode case-folding
"Hello, world!" and "Hello, WORLD!" are equal under Unicode case-folding
"Hello, world!" and "Hi, world!" are not equal under Unicode case-folding

Golang bytes Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.