How to Read from and Write to Bytes Buffers with bytes.Buffer

Use Write and Read methods to manipulate data in a bytes.Buffer for efficient in-memory string building.

Use Write and WriteString to add data, and Read, ReadString, or Bytes to retrieve it from a bytes.Buffer.

import "bytes"

var buf bytes.Buffer

// Write data
buf.WriteString("Hello, ")
buf.Write([]byte("World!"))

// Read data
content := buf.String() // Get full content as string
// or
data := buf.Bytes()     // Get full content as []byte
// or
buf.Reset()             // Clear buffer for reuse