There are two ways to compare strings lexicographically in Go:-
1. Use built-in comparison operators (officially recommended – more details below)
2. Use strings.Compare()
Compare Two Strings using Comparision Operators
In addition to numbers, Go comparison operators such as ==, >, >=, <, <=
also supports strings.
This is the officially recommended way to do string comparison in Go. According to the documentation for strings.Compare(),
Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
You’ll see these operators in action in the examples below, but here are some quick explanations on their expected behaviors when comparing strings.
The equality operator == returns true if the two strings are identical, or false if they're not.
>
returns true if the left string is lexicographically greater than the right string, or false otherwise.
>=
returns true if the left string is lexicographically greater than or equal to the right string, or false otherwise.
<
returns true if the left string is lexicographically less than the right string, or false otherwise
<=
returns true if the left string is lexicographically less than or equal to the right string, or false otherwise
[Example 1]
In this example, str1 and str2 contain the same string.
str1 := "abc"
str2 := "abc"
fmt.Println("str1 == str2 :", str1 == str2)
fmt.Println("str1 > str2:", str1 > str2)
fmt.Println("str1 >= str2:", str1 >= str2)
fmt.Println("str1 < str2 :", str1 < str2)
fmt.Println("str1 <= str2 :", str1 <= str2)
[Output]
str1 == str2 : true
str1 > str2: false
str1 >= str2: true
str1 < str2 : false
str1 <= str2 : true
[Example 2]
In this example, str1 (the left operand) is lexicographically greater than str2 (the right operand).
str1 := "def"
str2 := "abc"
fmt.Println("str1 == str2 :", str1 == str2)
fmt.Println("str1 > str2:", str1 > str2)
fmt.Println("str1 >= str2:", str1 >= str2)
fmt.Println("str1 < str2 :", str1 < str2)
fmt.Println("str1 <= str2 :", str1 <= str2)
[Output]
str1 == str2 : false
str1 > str2: true
str1 >= str2: true
str1 < str2 : false
str1 <= str2 : false
String Comparison using strings.Compare()
Another way to compare strings in Go is by using the Compare() function in the strings package.
As mentioned above, this method is not recommended. With that out of the way, let’s take a closer look at this function.
Function signature:
func Compare(a, b string) int
The Compare() function takes two strings a & b and returns an integer that is either -1, 0, or 1.
When the returned integer is 0, the two strings are equal.
When the result is -1, b is greater than a.
When the result is 1, b is lesser than a.
See it in action
[Example]
fmt.Println("strings.compare(\"abc12\", \"abc12\"):", strings.Compare("abc12", "abc12"))
fmt.Println("strings.compare(\"abc12\", \"abc13\"):", strings.Compare("abc12", "abc13"))
fmt.Println("strings.compare(\"abc13\", \"abc12\"):", strings.Compare("abc13", "abc12"))
[Output]
strings.compare("abc12", "abc12"): 0
strings.compare("abc12", "abc13"): -1
strings.compare("abc13", "abc12"): 1
Conclusion
The built-in operators are superior to strings.Compare() in almost every way. Personally, I find them intuitive and easier to remember.