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快,下面是註釋

/ 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

到此這篇關於golang 字符串比較是否相等的方法示例的文章就介紹到這瞭,更多相關golang 字符串比較相等內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!