Golang program to compare two strings


In go programming language, string is one of the data-types that can be used for various application. It is a collection of characters and is also immutable. In go programming language a string can't be modified after they have been created. In this article, we are going to study different techniques to compare two given strings.

Syntax

bytes.Equal(s1,s2)

Two slices of bytes ([]byte) are compared using the Equal() method to see if they are equal. If the two slices of bytes are equal, it produces a boolean result (true or false) reflecting that fact.

strings.Compare(s1,s2)

The Compare() function is used to lexicographically compare two strings. It gives back an integer value that represents the outcome of the comparison −

if the two strings are equivalent, a value of 0.

If the first string is lexicographically inferior to the second string, the value will be less than 0.

If the first string is lexicographically superior to the second string, the value will be greater than 0.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package

  • Step 2 − In the main function, create two variables

  • Step 3 − Assign the two strings you want to compare to them.

  • Step 4 − Compare both the strings using internal function or user-defined function

  • Step 5 − Print the output.

Example 1

In this example we will see how we can compare two strings using if-else statement in Golang.

package main
import (
   "fmt"
)
func main() {
   mystr1 := "create" //create str1
   fmt.Println("The string1 created here is:", mystr1)
   mystr2 := "craete" //create str2
   fmt.Println("The string2 created here is:", mystr2)
   fmt.Println("Are the strings equal?")
   if mystr1 == mystr2 {
      fmt.Println("The strings are equal.") //print strings are equal
   } else {
      fmt.Println("The strings are not equal.") //print strings are not equal
   }
}

Output

The string1 created here is: create
The string2 created here is: craete
Are the strings equal?
The strings are not equal.

Example 2

In this example we will use bytes.Equal() function to compare the two strings

package main
import (
   "bytes"
   "fmt"
)
func main() {
   mystr1 := "create" //create string1
   fmt.Println("The string1 created here is:", mystr1)
   mystr2 := "craete" //create string2
   fmt.Println("The string2 created here is:", mystr2)
   fmt.Println("Are the strings equal?")
   if bytes.Equal([]byte(mystr1), []byte(mystr2)) { //use bytes.Equal() function to compare slices
      fmt.Println("The strings are equal.") //print strings are not equal
   } else {
      fmt.Println("The strings are not equal.") //print strings are equal
   }
}

Output

The string1 created here is: create
The string2 created here is: craete
Are the strings equal?
The strings are not equal.

Example 3

In this example we will use strings.compare() function to compare two slices.

package main
import (
   "fmt"
   "strings"
)
func main() {
   mystr1 := "create" //create string1
   fmt.Println("The string1 created here is:", mystr1)
   mystr2 := "craete" //create string2
   fmt.Println("The string2 created here is:", mystr2)
   fmt.Println("Are the strings equal?")
   if strings.Compare(mystr1, mystr2) == 0 { //use strings.compare function
      fmt.Println("The strings are equal.") //print slices are equal
   } else {
      fmt.Println("The strings are not equal") //print slices are not equal
   }
}

Output

The string1 created here is: create
The string2 created here is: craete
Are the strings equal?
The strings are not equal

Conclusion

We executed the program of comparing two strings using three methods. In the first method we used simple comparison operator, in the second example we used bytes.Equal() function and in the third example we used strings.Compare() function.

Updated on: 13-Feb-2023

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements