Home » GO Programming Language » Concatenate Strings in Go Language Using strings.Builder and bytes.Buffer

Concatenate Strings in Go Language Using strings.Builder and bytes.Buffer

This tutorial shows how to concatenate strings in Go language using the strings.Builder and bytes.Buffer methods.

Example 1: Using strings.Builder Method

package main

import (
    "strings"
    "fmt"
)

func main() {
    
    var sb strings.Builder

    for i := 0; i < 1000; i++ {
        sb.WriteString("vin")
    }

    fmt.Println(sb.String())
}

Output:

vinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvin...

Example 2: Using the bytes.Buffer Method

package main

import (
    "bytes"
    "fmt"
)

func main() {
    var buffer bytes.Buffer

    for i := 0; i < 1000; i++ {
        buffer.WriteString("vin")
    }

    fmt.Println(buffer.String())
}

Output:

vinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvinvin...

See also: