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

daisy picture daisy · Jan 31, 2016 · Viewed 17.9k times · Source

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)

Answer

peterSO picture peterSO · Jan 31, 2016

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()