Home  >  Article  >  Backend Development  >  How to compare strings for equality in golang

How to compare strings for equality in golang

王林
王林Original
2019-12-28 10:37:419001browse

How to compare strings for equality in golang

1. Self-built method "==", case-sensitive, the simplest method

fmt.Println("go"=="go")
fmt.Println("GO"=="go")

Output result:

true
false

2. Compare Function, case-sensitive, faster than the self-built method "=="

fmt.Println(strings.Compare("GO","go"))
fmt.Println(strings.Compare("go","go"))

Output result:

-1
0

3. Compare UTF-8 encoding under lowercase conditions Whether they are equal or not, case-insensitive

fmt.Println(strings.EqualFold("GO","go"))

Output result:

true

Recommended related articles and tutorials: golang tutorial

The above is the detailed content of How to compare strings for 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
Previous article:How to debug golangNext article:How to debug golang