这种实现方式比使用 += 要更节省内存和 CPU,尤其是要串联的字符串数目特别多的时候。
2024-08-29 14:38:21
这种实现方式比使用 +=
要更节省内存和 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
}
最新文章
- Handler系列之内存泄漏
- Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem
- 细看INNODB数据落盘
- node.js 基础学习笔记3 -http
- fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev
- 如何使用 declare-styleable
- 【DP】permu
- git 在windows上 生成ssh公钥
- oracle 导出数据和导入数据
- target与currentTarget的区别?
- C++顺序容器类总结
- 【清澄A1333】【整体二分+二维树状数组】矩阵乘法(梁盾)
- java基础增强
- webapp之路--之ios上图标
- 学习面向对象编程OOP 第二天
- AWK求和、平均值、最值
- Python基础篇(五)
- mssql sqlserver存储过程专题
- 网站出现403 Forbidden
- PHP(层叠样式表,写法分类),选择器的种类)
热门文章
- 从客户端(ExportContent=";...ontinuous <;br />;Pass Count";:13...";)中检测到有潜在危险的 Request.Form 值。
- require_once(): Failed opening required &#39;/var/www/config/config.php&#39; (include_path=&#39;.:&#39;) in /var/www/vendor/forkiss/pharest/src/Pharest/Register/Register.php on line 10
- linux中shell脚本中系统预先定义的变量
- Linux多线程编程-信号量
- [Django]中建立数据库视图
- java把一个文件的内容复制到另外一个文件
- es6 - 模板
- es6 includes(), startsWith(), endsWith()
- Locust压力测试Odoo
- tomcat启动出现异常 Error filterStart