Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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) 
like image 958
daisy Avatar asked Jan 31 '16 05:01

daisy


People also ask

How to clear bytes buffer golang?

There are two ways to reset a buffer that with content ( length not equal zero ). You can use either Reset() or Truncate() methods found in the bytes. NewBuffer() function.

What is bytes buffer in Golang?

In go language, the buffer belongs to the byte package of the Go language, and we can use these package to manipulate the byte of the string. For example, suppose we have a string. We can read the length of the string with the len function, which will return the numeric length, but what if the strings are too large.


1 Answers

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() 
like image 85
peterSO Avatar answered Sep 30 '22 18:09

peterSO