How to Compare Two Strings in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


In this article, we will explore the different techniques for comparing strings in Go. We will discuss the basics of string comparison, including the available functions and their usage, as well as some advanced techniques to improve performance.

In Go, there are several ways to compare two strings. One of the most basic methods is to use the == operator, which compares the values of both strings directly. For example:

str1 := "hello"
str2 := "world"
if str1 == str2 {
    fmt.Println("Strings are equal")
} else {
    fmt.Println("Strings are not equal")
}

This code will compare the values of str1 and str2, and print a message indicating whether they are equal or not. However, this approach can be inefficient for large strings, as it requires comparing each character individually.

Another way to compare two strings is to use the strings.Compare() function, which returns an integer indicating the relative order of the strings. For example:

str1 := "hello"
str2 := "world"
if strings.Compare(str1, str2) == 0 {
    fmt.Println("Strings are equal")
} else {
    fmt.Println("Strings are not equal")
}

This code will compare the values of str1 and str2, and print a message indicating whether they are equal or not. This approach is more efficient than using the == operator, as it only requires comparing the relative order of the strings.

However, there may be situations where you need to compare two strings for equality without considering their case. In such cases, you can use the strings.EqualFold() function, which compares the values of both strings ignoring their case. For example:

str1 := "Hello"
str2 := "hello"
if strings.EqualFold(str1, str2) {
    fmt.Println("Strings are equal")
} else {
    fmt.Println("Strings are not equal")
}

This code will compare the values of str1 and str2, ignoring their case, and print a message indicating whether they are equal or not. This approach is useful when you need to compare two strings for equality without considering their case.

In addition to these basic techniques, there are also some advanced techniques that can be used to improve the performance of string comparison in Go. For example, if you know that one of the strings is a prefix of the other, you can use the strings.HasPrefix() function to check for equality more efficiently. For example:

str1 := "hello"
str2 := "world"
if strings.HasPrefix(str1, str2) {
    fmt.Println("Strings are equal")
} else {
    fmt.Println("Strings are not equal")
}

This code will check if str1 is a prefix of str2, and print a message indicating whether they are equal or not. This approach can be useful when you know that one of the strings is a prefix of the other, as it can reduce the number of comparisons required to determine equality.

In conclusion, there are several techniques for comparing two strings in Go, including using the == operator, strings.Compare() function, and strings.EqualFold() function. There are also some advanced techniques that can be used to improve the performance of string comparison, such as using strings.HasPrefix() function when one of the strings is a prefix of the other. By understanding these different approaches, you can choose the most appropriate technique for your specific use case and improve the performance of your Go programs.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!