字符串比较相等
func main() { fmt.Println("ADN" == "ADN") fmt.Println("ADN" == "adn") fmt.Println(strings.Compare("ADN", "ADN")) fmt.Println(strings.Compare("ADN", "adn")) fmt.Println(strings.EqualFold("ADN", "ADN")) fmt.Println(strings.EqualFold("ADN", "adn")) }
Compare比"=="快,两种方法都区分大小写
EqualFold比较UTF-8编码在小写的条件下是否相等,不区分大小写。
字符串拼接
fmt.Sprintf 和 strings.Join 速度相当
string + 比上述二者快一倍
bytes.Buffer又比上者快约400-500倍
如果循环内每次都临时声明一个bytes.Buffer来使用,会比持续存在慢50%,但是仍然很快
func main() { var c bytes.Buffer a := "ADN" + " so cool" b := strings.Join([]string{"ADN", "so", "cool"}, "-") c.WriteString("ADN ") c.WriteString("so ") c.WriteString("cool") fmt.Println(a) fmt.Println(b) fmt.Println(c.String()) }
作者:ifcoder
链接:https://www.jianshu.com/p/05a55a633de9