这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候。

package main

import (
"bytes"
"fmt"
) func main() {
a := [...]string{"a", "b", "c"}
var buffer bytes.Buffer
for _, v := range a {
buffer.WriteString(v)
}
str := buffer.String()
fmt.Println(str)
buffer.Reset()
str = buffer.String()
fmt.Println(str)
fmt.Println("343") for _, v := range a {
buffer.WriteString(v)
}
buffer.Truncate(2)
str = buffer.String()
fmt.Println(str) for _, v := range a {
buffer.WriteString(v)
}
buffer.Truncate(0)
str = buffer.String()
fmt.Println(str)
}
// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as Truncate(0).
func (b *Buffer) Reset() {
b.buf = b.buf[:0]
b.off = 0
b.lastRead = opInvalid
}

最新文章

  1. Handler系列之内存泄漏
  2. Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem
  3. 细看INNODB数据落盘
  4. node.js 基础学习笔记3 -http
  5. fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev
  6. 如何使用 declare-styleable
  7. 【DP】permu
  8. git 在windows上 生成ssh公钥
  9. oracle 导出数据和导入数据
  10. target与currentTarget的区别?
  11. C++顺序容器类总结
  12. 【清澄A1333】【整体二分+二维树状数组】矩阵乘法(梁盾)
  13. java基础增强
  14. webapp之路--之ios上图标
  15. 学习面向对象编程OOP 第二天
  16. AWK求和、平均值、最值
  17. Python基础篇(五)
  18. mssql sqlserver存储过程专题
  19. 网站出现403 Forbidden
  20. PHP(层叠样式表,写法分类),选择器的种类)

热门文章

  1. 从客户端(ExportContent="...ontinuous <br />Pass Count":13...")中检测到有潜在危险的 Request.Form 值。
  2. require_once(): Failed opening required '/var/www/config/config.php' (include_path='.:') in /var/www/vendor/forkiss/pharest/src/Pharest/Register/Register.php on line 10
  3. linux中shell脚本中系统预先定义的变量
  4. Linux多线程编程-信号量
  5. [Django]中建立数据库视图
  6. java把一个文件的内容复制到另外一个文件
  7. es6 - 模板
  8. es6 includes(), startsWith(), endsWith()
  9. Locust压力测试Odoo
  10. tomcat启动出现异常 Error filterStart