1. 程式人生 > >golang 字串比較是否相等

golang 字串比較是否相等

golang字串比較的三種常見方法

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

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

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

輸出

true
false
-1
0
true

1,自建方法“==”,區分大小寫,最簡單的方法

2,Compare函式,區分大小寫,比自建方法“==”的速度要快,下面是註釋
/ Compare is included only for symmetry with package bytes.
// It is usually clearer and always faster to use the built-in
// string comparison operators ==, <, >, and so on.
func Compare(a, b string) int

3,比較UTF-8編碼在小寫的條件下是否相等,不區分大小寫,下面是註釋
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func EqualFold(s, t string) bool