In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. You are allowed to compare strings with each other using two different ways:
1. Using comparison operators: Go strings support comparison operators, i.e, ==, !=, >=, <=, <, >. Here, the == and != operator are used to check if the given strings are equal or not, and >=, <=, <, > operators are used to find the lexical order. The results of these operators are of Boolean type, meaning if the condition is satisfied it will return true, otherwise, return false.
Result 1: false Result 2: false Result 3: false Result 4: true Result 5: true Result 6: true Result 7: true Result 8: false
Slice: [Geeks Geeks gfg GFG for] Result 1: false Result 2: true Result 3: false Result 4: true Result 5: true Result 6: true
2. Using Compare() method: You can also compare two strings using the built-in function Compare() provided by the strings package. This function returns an integer value after comparing two strings lexicographically. The return values are:
- Return 0, if str1 == str2.
- Return 1, if str1 > str2.
- Return -1, if str1 < str2.
func Compare(str1, str2 string) int
1 0 1 -1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.