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

How to compare strings for equality in golang

王林
Release: 2019-12-28 10:37:41
Original
9053 people have browsed it

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")
Copy after login

Output result:

true
false
Copy after login

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

fmt.Println(strings.Compare("GO","go"))
fmt.Println(strings.Compare("go","go"))
Copy after login

Output result:

-1
0
Copy after login

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

fmt.Println(strings.EqualFold("GO","go"))
Copy after login

Output result:

true
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template