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 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts