OneBite.Dev - Coding blog in a bite size

compare two strings in Go

Code snippet on how to compare two strings in Go

// Code for comparing two strings
if strings.Compare(string1, string2) == 0 {
    fmt.Println("The strings are equal.")
} else {
    fmt.Println("The strings are not equal.")
}

This code is written in the Go programming language and is used to compare two strings, string1 and string2. It begins by using the strings.Compare() function, which compares two strings and returns 0 if they are equal, or a non-zero value if they are not equal. The code then determines whether strings.Compare returned a 0 or some other non-zero value – if it is 0, the strings are equal and “The strings are equal.” is printed; otherwise, the strings are not equal, so “The strings are not equal.” is printed.

go