0Pricing
Go Academy · Lesson

strings.Builder

Build strings efficiently.

strings.Builder is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Cost of + Concatenation

Because strings are immutable, every s = s + part creates a brand new string and copies all the bytes again.

In a loop this becomes slow and wastes memory.

package main

import "fmt"

func main() {
    s := ""
    for i := 0; i < 3; i++ {
        s = s + "go "
    }
    fmt.Println(s)
}

Meet strings.Builder

strings.Builder grows an internal buffer and avoids repeated copying. You write into it, then read the result once at the end.

Declare one with var b strings.Builder.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.WriteString("Hello")
    fmt.Println(b.String())
}

WriteString in a Loop

The real win shows up in loops. Call WriteString repeatedly, then read once with String().

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    for i := 0; i < 3; i++ {
        b.WriteString("go ")
    }
    fmt.Println(b.String())
}

Writing Bytes and Runes

A Builder accepts more than full strings:

  • WriteByte adds a single byte.
  • WriteRune adds one Unicode code point.
package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.WriteString("Go")
    b.WriteByte('!')
    b.WriteRune('語')
    fmt.Println(b.String())
}

Builder Implements io.Writer

Because Builder satisfies the io.Writer interface, you can use it with fmt.Fprintf to format directly into the buffer.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    fmt.Fprintf(&b, "x=%d y=%d", 3, 4)
    fmt.Println(b.String())
}

Tracking Length

b.Len() tells you how many bytes have been written so far, without building the final string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.WriteString("hello")
    fmt.Println(b.Len())
}

Preallocating with Grow

If you know roughly how big the result will be, call b.Grow(n) first. This reserves capacity and avoids repeated buffer resizing.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.Grow(64)
    for i := 0; i < 5; i++ {
        b.WriteString("item ")
    }
    fmt.Println(b.String())
}

Resetting a Builder

b.Reset() empties the buffer so you can reuse the same Builder for the next string.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.WriteString("first")
    b.Reset()
    b.WriteString("second")
    fmt.Println(b.String())
}

Do Not Copy a Builder

A strings.Builder must not be copied after first use. Pass it around using a pointer (*strings.Builder) instead.

package main

import (
    "fmt"
    "strings"
)

func add(b *strings.Builder, word string) {
    b.WriteString(word)
}

func main() {
    var b strings.Builder
    add(&b, "shared ")
    add(&b, "builder")
    fmt.Println(b.String())
}

Building a CSV Line

A practical example: assemble a comma-separated line efficiently with a Builder.

package main

import (
    "fmt"
    "strings"
)

func main() {
    fields := []string{"id", "name", "age"}
    var b strings.Builder
    for i, f := range fields {
        if i > 0 {
            b.WriteByte(',')
        }
        b.WriteString(f)
    }
    fmt.Println(b.String())
}

When to Use a Builder

Reach for strings.Builder when you concatenate many pieces, especially inside loops.

For a one-off join of a known slice, strings.Join is simpler and just as fast.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    for i := 1; i <= 3; i++ {
        fmt.Fprintf(&b, "[%d]", i)
    }
    fmt.Println(b.String())
}

Quick Check

Why prefer a Builder over repeated + in a loop?

Recap: strings.Builder

You learned to build strings efficiently:

  • Declare with var b strings.Builder.
  • Append via WriteString, WriteByte, WriteRune, or fmt.Fprintf.
  • Optionally Grow ahead of time, read once with String(), and never copy it.
package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    b.Grow(16)
    b.WriteString("Go ")
    b.WriteString("builder!")
    fmt.Println(b.String())
}

Frequently asked questions

Is the “strings.Builder” lesson free?

Yes — the full text of “strings.Builder” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “strings.Builder”?

Build strings efficiently. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “strings.Builder” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Runes vs Bytes
  2. strings Package Functions
  3. strings.Builder
  4. Unicode and utf8
← Back to Go Academy