Home  >  Article  >  Backend Development  >  How to determine string equality in Golang?

How to determine string equality in Golang?

WBOY
WBOYOriginal
2024-03-13 15:24:04974browse

How to determine string equality in Golang?

How to determine string equality in Golang?

In Golang, determining whether two strings are equal is a common operation. In this article, we will introduce several commonly used methods to determine whether two strings are equal and give corresponding code examples.

Method 1: Use the == operator

The simplest way is to use the == operator to determine whether two strings are equal . An example is as follows:

package main

import "fmt"

func main() {
    str1 := "Hello"
    str2 := "Hello"
    
    if str1 == str2 {
        fmt.Println("str1 和 str2 相等")
    } else {
        fmt.Println("str1 和 str2 不相等")
    }
}

In the above example, we created two strings str1 and str2 and then used the == operation symbol to compare whether they are equal. If they are equal, output "str1 and str2 are equal", otherwise output "str1 and str2 are not equal".

Method 2: Use strings.EqualFold function

In addition to using the == operator, we can also use strings.EqualFold Function to determine whether two strings are equal. This function compares two strings regardless of case. An example is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str1 := "Hello"
    str2 := "hello"
    
    if strings.EqualFold(str1, str2) {
        fmt.Println("str1 和 str2 相等")
    } else {
        fmt.Println("str1 和 str2 不相等")
    }
}

In the above example, we use the strings.EqualFold function to compare whether str1 and str2 are equal. The function ignores case, so the output is "str1 and str2 are equal".

Method 3: Use the strings.Compare function

Another method is to use the strings.Compare function to compare the sizes of two strings . If the two strings are equal, the function returns 0. The example is as follows:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str1 := "Hello"
    str2 := "Hello"
    
    result := strings.Compare(str1, str2)
    
    if result == 0 {
        fmt.Println("str1 和 str2 相等")
    } else {
        fmt.Println("str1 和 str2 不相等")
    }
}

In the above example, we use the strings.Compare function to compare whether str1 and str2 are equal. The strings are equal, so the output result is "str1 and str2 are equal".

Through the above methods, we can easily determine whether two strings are equal in Golang. Choosing the appropriate method can be determined based on specific needs to achieve the best judgment.

The above is the detailed content of How to determine string equality in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn