🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 常用字符串连接方法 * 使用+拼接字符串 ` result += "hello" + ","` * strings.Builder (go1.10+) * bytes.Buffer ## 几种字符串操作benchmark示例 ~~~ func BenchmarkStringJoin(b *testing.B) { result := "" b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < OP_NUM; j++ { result += strconv.Itoa(j) + "," } } b.StopTimer() } func BenchmarkStringBuilder(b *testing.B) { var sb strings.Builder b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < OP_NUM; j++ { sb.WriteString(strconv.Itoa(j)) sb.WriteString(",") } } b.StopTimer() } func BenchmarkStringBuffer(b *testing.B) { var buf bytes.Buffer for i := 0; i < b.N; i++ { for j := 0; j < OP_NUM; j++ { buf.WriteString(strconv.Itoa(j)) buf.WriteString(",") } } b.StopTimer() } ~~~