AppDividend
Latest Code Tutorials

How To Compare Strings in Golang Example

Golang strings.Compare() is an inbuilt method that returns an integer comparing two strings lexicographically. The result will be

  1. 0 if a==b
  2. -1 if a < b
  3. +1 if a > b

The Compare() function is included only for symmetry with package bytes. It is usually clearer and always faster to use the inbuilt string comparison operators ==, <, >, and so on.

How To Compare Strings in Golang

In Golang, you can compare the string using three ways.

  1. Using strings.Compare() method
  2. Using (==) operator
  3. Using strings.EqualFold() method

Golang strings.Compare()

See the following syntax.

func Compare(a, b string) int

See the following code.

package main

import (
	"fmt"
	"strings"
)

func main() {
	// Comparing two strings using Compare() function
	fmt.Println("First Output:", strings.Compare("apd", "AppDividend"))

	fmt.Println("Second Output:", strings.Compare("AppDividend",
		"AppDividend"))

	fmt.Println("Third Output:", strings.Compare("AppDividend", "appdividend"))
}

Output

go run hello.go
First Output: 1
Second Output: 0
Third Output: -1

In the first output, it returns 1 because is greater than A.

In the second output, it returns 0 because both strings are great case-wise.

In the third output, it returns -1 because A is less than a.

Using comparison operators

Golang strings support comparison operators, i.e, ==, !=, >=, <=, <, >. Here, == and != operator is used to check if the given strings are equal or not. and >=, <=, <, > operators are used to finding the lexical order.

The result of these operators is of Boolean type, which means if the condition satisfies, it will return true; otherwise, return false.

See the following code.

package main

import "fmt"

// Main function
func main() {

	// Creating and initializing strings
	// using shorthand declaration
	str1 := "Apps"
	str2 := "App"
	str3 := "AppDividend"
	str4 := "Apps"

	// Checking the string are equal
	// or not using == operator
	result1 := str1 == str2
	result2 := str2 == str3
	result3 := str3 == str4
	result4 := str1 == str4

	fmt.Println("Result 1: ", result1)
	fmt.Println("Result 2: ", result2)
	fmt.Println("Result 3: ", result3)
	fmt.Println("Result 4: ", result4)

	// Checking the string are not equal
	// using != operator
	result5 := str1 != str2
	result6 := str2 != str3
	result7 := str3 != str4
	result8 := str1 != str4

	fmt.Println("\nResult 5: ", result5)
	fmt.Println("Result 6: ", result6)
	fmt.Println("Result 7: ", result7)
	fmt.Println("Result 8: ", result8)

}

Output

go run hello.go
Result 1:  false
Result 2:  false
Result 3:  false
Result 4:  true

Result 5:  true
Result 6:  true
Result 7:  true
Result 8:  false

In the above program, first and the last variable has the same value, that is why it returns true. We have determined the results using comparison operators.

Using strings.EqualFold()

Uppercase letters are not equal to lowercase letters. But with EqualFold, we fold cases. This makes “A” equal to “a.” No string copies or conversions are required.

Using EqualFold instead of creating many strings by lowercasing them can optimize performance.

See the following code.

// hello.go

package main

import (
	"fmt"
	"strings"
)

func main() {
	value1 := "dog"
	value2 := "Dog"

	// This returns true because cases are ignored.
	if strings.EqualFold(value1, value2) {
		fmt.Println(true)
	}
}

Output

go run hello.go
true

From the above program, you can see that string.EqualsFold() function ignores cases and returns true because it assumes both strings are the same regardless of uppercase and lowercase.

See also

Convert Golang string to int
Convert Golang JSON to Map
Convert Golang int to string

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.