Welcome To Golang By Example

string compare in Go (Golang)

Table of Contents

Overview

In Golang string are UTF-8 encoded. strings package of GO provides a Compare method that can be used to compare two strings in Go. Note that this method compares strings lexicographically.

Below is the signature of the function

func Compare(a, b string) int

As you can notice the return value of the Compare function is an int. This value will be

So if the return value is 0 then the two strings are equal. Let’s look at a working program

Code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    res := strings.Compare("abc", "abc")
    fmt.Println(res)

    res = strings.Compare("abc", "xyz")
    fmt.Println(res)

    res = strings.Compare("xyz", "abc")
    fmt.Println(res)
}

Output:

0
-1
1