Go - compare strings example

In this example, we show you how to compare two strings in the case-sensitive and case-insensitive manner in Golang.

Strings are compared with the == operator. Case-insensitive comparisons are performed with the EqualFold function.

Go - compare strings example

The example compares two strings in a case-sensitive and case-insensitive manner in Golang.


package main

import (
    "fmt"
    "strings"
)

func main() {

    w1 := "SOURCEcodeexamples"
    w2 := "sourcecodeexamples"

    if w1 == w2 {

        fmt.Println("the strings are equal")
    } else {

        fmt.Println("the strings are not equal")
    }

    
    if strings.EqualFold(w1, w2) {

        fmt.Println("the strings are equal")
    } else {

        fmt.Println("the strings are not equal")
    }
}

Output:

the strings are not equal
the strings are equal

Comments