Python 的 io.BytesIO 的 Go 等价物
Python 3.x 中的 io.BytesIO 提供了一种方便的方式,拥有一个实际从/向内存流式传输的类文件对象。
在 Go 中,等价的问题是拥有一个由 []byte 或 string 支持的 io.Reader 和/或 io.Writer(即 Python 类文件对象的等价物)。
解决方案:使用 bytes.Buffer!
go_bytes_buffer_examples.go
// 初始化空缓冲区(例如用于写入)
buf := &bytes.Buffer{}
// 用 []byte 内容初始化缓冲区(例如用于读取)
myBytes := ...
buf := bytes.NewBuffer(myBytes)
// 用字符串初始化缓冲区
myStr := "test 123"
buf := bytes.NewBufferString(myStr)Check out similar posts by category:
Go
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow