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 string supprot comparasion coperators, i.e, ==, !=, >=, <=, <, >. Here, == and != operator is used to check if the given strings are equal or not. and >=, <=, <, > opeartors are used to find the lexical order. The result of these opertors are of Boolean type, means if the condition satisfy 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 string using inbuilt function Compare() provide by the string package. This function return 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
- strings.Compare() Function in Golang with Examples
- Different ways to concatenate two strings in Golang
- How to compare times in Golang?
- How to compare two slices of bytes in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- Compare Println vs Printf in Golang with Examples
- How to compare Structs with the Different Values Assigned to Data Fields in Golang?
- Different Ways to Find the Type of Variable in Golang
- Different Ways to Find the Type of an Object in Golang
- Different Ways to Convert an Integer Variable to String in Golang
- Different Ways to Convert the Boolean Type in String in Golang
- Strings in Golang
- How to Sort a Slice of Strings in Golang?
- strings.Map() Function in Golang With Examples
- strings.FieldsFunc() Function in Golang With Examples
- strings.IndexFunc() Function in Golang With Examples
- strings.ToValidUTF8() Function in Golang With Examples
- strings.Replace() Function in Golang With Examples
- strings.ContainsAny() Function in Golang with Examples
- strings.ToTitleSpecial() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.