Go

Go

By DeepSource

Use (*bytes.Buffer).Reset instead GO-R1003

Anti-pattern

Truncate discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. If n is 0, i.e., argument to Truncate, it is the same as Reset that resets the buffer to empty, and the latter is considered more idiomatic.

Examples

Bad practice

buf := &bytes.Buffer{}
// do some work
buf.Truncate(0)

Recommended

buf := &bytes.Buffer{}
// do some work
buf.Reset()