7 Ways to Concatenate Strings in Golang

String concatenation is one of the most essential aspects of programming. Strings are building blocks of programming. In this post, we will explore different ways to concatenate strings in Golang programming language.

1. Golang String concatenation using the plus operator

The plus (+) operator can be used to concatenate strings. It is generally the most used way of string concatenation without much thinking. It is also pretty obvious.

name := "John" + " " + "Doe" // John Doe

The print functions inside the fmt package automatically concatenate whenever multiple strings are provided as an argument. It also adds space between to strings.

fmt.Println("It", "works!") // prints "It works!"

2. String Concatenation using string append

The strings can be appended to one another by using the += operator. It is the same as above. But a slightly shorter way of concatenating. It appends the right side to the string it is operated on. So, it essentially appends string. Below is an example of appending strings using the plus-equal operator.

u := "This"
v := " is working."
u += v   // sets u to "This is working."

3. Strings join() function to concatenate two strings

The join function from the strings package can be used to concatenate strings. It has a method signature as shown below.

func Join(a []string, sep string) string

It takes two arguments, a slice of strings and a separator to join them with. It will produce one single string from it. Here is an example showing how it works.

package main

import (
	"fmt"
	"strings"
)

func main() {

	s := []string{"This", "is", "a", "string."}

	v := strings.Join(s, " ")
	
	fmt.Println(v)  // This is a string.
}

4. The Sprintf() method to concatenate strings in Go

The Sprintf() method in the fmt package can be used for string concatenation. There is a simple way of doing it using that. Here is the way to get the concatenated string out of it.

package main

import (
	"fmt"
)

func main() {

	s1 := "abc"
	s2 := "xyz"
	
	v := fmt.Sprintf("%s%s", s1, s2)
	
	fmt.Println(v) // abcxyz
}

As you can see the string formatting allows us to do concatenation that way.

5. The bytes buffer method

The bytes package contains a type of buffer, the byte buffer. We can write strings with the WriteString method and then transform it into a string. This is an efficient way of concatenating strings. Here is an example of the byte buffer method of string concatenation.

package main

import (
	"fmt"
	"bytes"
)

func main() {
	var b bytes.Buffer
	
	b.WriteString("abc")
	b.WriteString("def") // append
	
	fmt.Println(b.String()) // abcdef
}

6. Go strings builder method to concatenate strings

The strings package has a builder type which is a very efficient way of building strings. It uses much less memory when concatenating strings and is a better way of concatenation. The function WriteString allows us to concatenate strings in a faster way. Below is an example of string concatenation using the strings builder method.

package main

import (
	"fmt"
	"strings"
)

func main() {
	var sb strings.Builder
	sb.WriteString("First")
	sb.WriteString("Second")
	fmt.Println(sb.String())    // FirstSecond
}

Using the same builder type we can add runes or bytes to the string.

7. Repeat() Method to concatenate same string multiple times

We can join the same string multiple times to form another string. The repeat method from the strings package does it in an efficient way. Here is the usage of the function.

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Repeat("abc", 3))  // abcabcabc
}

We can see that there is a multitude of ways string concatenation can be done in Golang.