Golang Program to Differentiate String == operator and equals() method


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. The == operator in Go is used to determine whether two values, including strings, are equal. The == operator determines whether two operands are equal by comparing their values and returning a boolean value. On the other hand, the strings package's EqualFold() method examines two strings case-insensitively and returns a boolean value indicating whether or not they are equal.

Method 1: Using bytes.Equal() method

In this example we will see how to differentiate the string== operator and equals() method. Here, the functions []byte(mystr1) and []byte(mystr2) translate the strings into slices of bytes and then back to bytes. The equal() function compares the two slices and produces a true or false boolean value. It's crucial to note that since this method compares the bytes representation of the strings, the outcome could vary based on the string's encoding compared to using the "==" operator.

Syntax

bytes.Equal()

In this method two slices of bytes are compared for equality using the equal method. If the slices are equal or not, a Boolean value is returned. The bytes package contains the method's definition.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and bytes package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a string named mystr1 and mystr2. Print them on the console.

  • Step 3 − Use bytes.Equal method to check whether the slice converted to bytes are equal or not.

  • Step 4 − This method will return a Boolean value as false or true.

  • Step 5 − The output will be printed on the console using fmt.Println() function where ln means new line.

Example

In this example, we are going to use byte.Equal() function of golang.

package main
import (
   "bytes"
   "fmt"
)

func main() {
   mystr1 := "hello"  //created string1
   fmt.Println("The string1 created here is:", mystr1)
   mystr2 := "alexa" //created string2
   fmt.Println("The string2 created here is:", mystr2)
   fmt.Println("Are the strings equal?")
   
   // Using bytes.Equal() method
   fmt.Println(bytes.Equal([]byte(mystr1), []byte(mystr2))) // false
}

Output

The string1 created here is: hello
The string2 created here is: alexa
Are the strings equal?
false

Method 2: Using EqualFold method

In this example we will see how to differentiate string == operator and equals method. Here, the second comparison using strings will result in true for the initial comparison using the == operator. True will be returned by EqualFold(). Let’s see through Example and algorithm to get a crystal-clear understanding of the concept.

Syntax

strings.EqualFold()

The case of the characters is not taken into account when using the EqualFold function to compare two strings for equality. If the two strings are equal (true), the function returns a Boolean result, else it returns a false value (false).

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function declare two string variables, mystr1 and mystr2, and initialize them with some values.

  • Step 3 − Use the == operator to compare mystr1 and mystr2.

  • Step 4 − If mystr1 and mystr2 are equal, print "str1 and str2 are equal" to the console. If they are not equal, print "str1 and str2 are not equal".

  • Step 5 − Use EqualFold() function to compare str1 and str2 case-insensitively.

  • Step 6 − If str1 and str2 are equal, print "str1 and str2 are equal" to the console. If they are not equal, print "str1 and str2 are not equal".

  • Step 7 − The print statement is executed using fmt.Println() function where ln means new line.

Example

In the following example, we are going to use EqualFold method

package main
import (
   "fmt"
   "strings"
)

func main() {
   mystr1 := "create"
   fmt.Println("The string1 created here is:", mystr1)
   mystr2 := "craete"
   fmt.Println("The string2 created here is:", mystr2)
   
   // Using == operator
   fmt.Println("Are the strings equal using == operator?")
   if mystr1 == mystr2 {
      fmt.Println("str1 and str2 are equal")
   } else {
      fmt.Println("str1 and str2 are not equal")
   }
   
   // Using strings.EqualFold() method
   fmt.Println("Are the strings equal using EqualFold method?")
   if strings.EqualFold(mystr1, mystr2) {
      fmt.Println("str1 and str2 are equal")
   } else {
      fmt.Println("str1 and str2 are not equal")
   }
}

Output

The string1 created here is: create
The string2 created here is: craete
Are the strings equal using == operator?
str1 and str2 are not equal
Are the strings equal using EqualFold method?
str1 and str2 are not equal

Conclusion

We executed the program of differentiating the == operator and equals method using two examples. In the first example we used bytes.Equal() method and in the second example we used EqualFold method to execute the program.

Updated on: 20-Feb-2023

666 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements