How to concatenate strings in Golang?
May 25, 2022Hi 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.
- Intilize string builder as variable
- Use variable with
WriteString()
method to concat - 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.
All the best 👍