Skip to content

Golang Concatenate Strings [Efficient Way – Builder]

Ok, we have a huge amount of strings to join them to each other! and maybe from different types!

To concatenate strings efficiently my recommendation is Builder from the string package:

// Add to builder
func (b *Builder) WriteString(s string) (int, error)
func (b *Builder) WriteByte(c byte) error {

// Get the string
func (b *Builder) String() string {

package main

import (
	"fmt"
	"strings"
)

func main() {
	strs := []string{
		"a",
		"b",
		"c",
		"d",
	}

	strBuilder := strings.Builder{}

	for _, s := range strs {
		strBuilder.WriteString(s)
	}

	fmt.Println(strBuilder.String())
}

Leave a Reply

Your email address will not be published. Required fields are marked *