Compare two String in Golang

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Check this out if you want to learn more about Strings in Go. In Go, string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding format.

String comparision is a way of comparing two strings to check its equality. There are several ways to comparestrings in Go.

Go string supports comparision operators like ==, !=, <=, >=, <, >.

Here are some methods to compare strings in Go-

Method 1 : String eqality comparision operators == and !=

For checking equality we can use == and !=. == checks if two strings are equal, != checks if two strings are not equal.

package main

import "fmt"

func main() {

    // Creating and initializing strings
    // using shorthand declaration
    str1 := "Debug"
    str2 := "Debug"
    str3 := "Pointer"

    result1 := str1 == str2
    result2 := str2 == str3
    result3 := str1 == str3

    fmt.Println("Result 1 (str1 == str2): ", result1)
    fmt.Println("Result 2 (str2 == str3): ", result2)
    fmt.Println("Result 3 (str1 == str3): ", result3)

    result5 := str1 != str2
    result6 := str2 != str3
    result7 := str1 != str3

    fmt.Println("\n Result 5 (str1 != str2): ", result5)
    fmt.Println("Result 6 (str2 != str3): ", result6)
    fmt.Println("Result 7 (str1 != str3): ", result7)

}
Result 1 (str1 == str2):  true
Result 2 (str2 == str3):  false
Result 3 (str1 == str3):  false

 Result 5 (str1 != str2):  false
Result 6 (str2 != str3):  true
Result 7 (str1 != str3):  true

Program exited.

Method 2 : String Comparision Operators

package main

import "fmt"

func main() {

    names:= []string{"Chuck", "Rock",
                    "Rick", "Gary", "Lisa"}

    fmt.Println("Names: ", names)

    result1 := "Chuck" > "Rock"
    fmt.Println("Result 1: ", result1)

    result2 := "Chuck" < "Rock"
    fmt.Println("Result 2: ", result2)

    result3 := "Rock" >= "Lisa"
    fmt.Println("Result 3: ", result3)

    result4 := "Rock" <= "Lisa"
    fmt.Println("Result 4: ", result4)

    result5 := "Rock" == "Rock"
    fmt.Println("Result 5: ", result5)

    result6 := "Rock" != "Lisa"
    fmt.Println("Result 6: ", result6)

}
Names:  [Chuck Rock Rick Gary Lisa]
Result 1:  false
Result 2:  true
Result 3:  true
Result 4:  false
Result 5:  true
Result 6:  true

Program exited.

Method 3 : Using Compare() method

You can compare two strings using the Compare() method that is inbuilt in the Go Programming Language. The Compare() method returns an integer value after it performs operation on two strings lexicographically. The value of the integer is is positive, negative or zero depending on how you compare the strings(position).

Syntax

func Compare(str1, str2 string) int
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Compare("DebugPointer", "StackOverflow"))

    fmt.Println(strings.Compare("DebugPointer",
                               "DebugPointer"))

    fmt.Println(strings.Compare("Debug", " Debugging"))

    fmt.Println(strings.Compare("DebugPointer", "Debugpointer"))

}
-1
0
1
-1

Program exited.