Go Reference ยท strings and strconv
Go strings.Builder type
Reference for Go strings.Builder: build large strings without repeated copying, use WriteString, WriteRune, Grow and Reset, and avoid the copy-by-value panic.
strings.Builder is a growable buffer for assembling a string piece by piece. Because Go strings are immutable, s += part in a loop copies the whole string every time and does quadratic work. A Builder appends into one internal byte slice and hands you the final string at the end without copying it again.
The zero value is ready to use, so you never need a constructor. A *Builder also implements io.Writer, which means you can pass it to fmt.Fprintf, io.Copy and anything else that writes.
var sb strings.Builder sb.WriteString("Hello") sb.WriteByte(',') sb.WriteRune('โบ') fmt.Println(sb.String())
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
type Builder struct { /* unexported fields */ } func (b *Builder) WriteString(s string) (int, error) func (b *Builder) WriteByte(c byte) error func (b *Builder) WriteRune(r rune) (int, error) func (b *Builder) Write(p []byte) (int, error) func (b *Builder) String() string func (b *Builder) Len() int func (b *Builder) Cap() int func (b *Builder) Grow(n int) func (b *Builder) Reset()
| Method | Description |
|---|---|
WriteString, Write, WriteByte, WriteRune |
Append text. The returned error is always nil; the signatures exist to satisfy io interfaces. |
String |
Returns the accumulated text. It does not copy the data. |
Len, Cap |
Bytes written so far, and the bytes available before the next reallocation. |
Grow(n) |
Makes room for at least n more bytes. Panics if n is negative. |
Reset |
Empties the builder and drops the buffer so the memory can be freed. |
Usage notes
- Do not copy a used Builder. Copying one by value and writing to the copy panics with an "illegal use of non-zero Builder copied by value" message. Pass
*strings.Builderor keep it in a struct that you pass by pointer. - Call
Growwhen you know the size. If you can estimate the final length, oneGrowcall avoids several reallocations. String()is cheap but not a snapshot you can mutate. The returned string shares the builder's memory safely because the builder only ever appends. AfterResetthe old string stays valid.- Check the error? Not needed. Writes to a
Buildernever fail, so ignoring the return values is normal. - Not safe for concurrent use. Guard it with a mutex or give each goroutine its own builder.
- For bytes you also want to read back, use
bytes.Buffer.Builderis write-only and produces strings;Buffercan be read from. - Small joins: for a slice of strings,
strings.Joinis simpler than a loop with a builder.