What's the right way to clear a bytes.Buffer in golang?

huangapple go评论84阅读模式
英文:

What's the right way to clear a bytes.Buffer in golang?

问题

我正在尝试清空一个bytes.Buffer,但是文档中没有这样的函数。

也许我应该重新创建一个新的缓冲区?应该如何正确地做呢?

buffer = bytes.NewBufferString("")
buffer.Grow(30000)
英文:

I'm trying to clear a bytes.Buffer, but there's no such function in the document

Maybe I should just renew the buffer? What's the right way to do it?

buffer   = bytes.NewBufferString("")
buffer.Grow (30000)

答案1

得分: 48

> 字节包
>
> func (*Buffer) Reset
>
> func (b *Buffer) Reset()
>
> Reset 重置缓冲区,使其不包含任何内容。b.Reset() 等同于 b.Truncate(0)。
>
> func (*Buffer) Truncate
>
> func (b *Buffer) Truncate(n int)
>
> Truncate 从缓冲区中丢弃除前 n 个未读字节之外的所有内容。如果 n 为负数或大于缓冲区的长度,则会引发 panic。

buffer.Reset()
英文:

> Package bytes
>
> func (*Buffer) Reset
>
> func (b *Buffer) Reset()
>
> Reset resets the buffer so it has no content. b.Reset() is the same as
> b.Truncate(0).
>
> func (*Buffer) Truncate
>
> func (b *Buffer) Truncate(n int)
>
> Truncate discards all but the first n unread bytes from the buffer. It
> panics if n is negative or greater than the length of the buffer.

buffer.Reset()

huangapple
  • 本文由 发表于 2016年1月31日 13:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/35110610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定