How to concatenate strings in Golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

Today, I’m going to show how do you efficiently concatenate strings in golang, here I will use strings.Builder to access the feature of string concat.

The strings.Builder provide the WriteString() method which we are going to use to concat two or multiple strings.

Well, let’s start today’s tutorial How to concatenate strings in Golang?

Before writing concatenates strings to code, let’s plan out what we are going to do.

  1. Intilize string builder as variable
  2. Use variable with WriteString() method to concat
  3. Print concatenate in golang console.
package main

import (
	"fmt"
	"strings"
)

func main() {
	// ZERO-VALUE:
	//
	// It's ready to use from the get-go.
	// You don't need to initialize it.

	var str strings.Builder
	
	str.WriteString("a")
	str.WriteString("G")
	str.WriteString("u")
	str.WriteString("i")
	str.WriteString("d")
	str.WriteString("e")
	str.WriteString("H")
	str.WriteString("u")
	str.WriteString("b")

	fmt.Println(str.String())
}

In the above example, we have used strings.Builder to concat aGuideHub string char, and it should show in the golang console.

Let’s check the output.

get, concatenate strings, golang

Try it yourself

All the best 👍

Follow me on Twitter