String concatenation refers to the process of combining two or more strings to form a single string. It is one of the essential string operations in programming.

In this guide, we will implode various methods of performing string concatenation in the Go programming language.

The Basics

Let us start off with the most basic parts of all. How to create a string in go?

We can create a string by enclosing the characters in double-quotes.

For example, the code below creates a string and stores it to the variable str.

Using double quotes creates an interpreted string literal that includes special characters such as whitespaces.

To create a literal string, you can use back-ticks, as shown in the example below:

A literal string in go does not support the use of special characters, including escape sequences.

String Concatenation – The addition Operator

If you are looking for the painless and easiest way to concatenate strings, you will probably opt for the plus (| ) operator.

Using the plus ( ) operator, you can combine multiple strings to a single string value as shown:

package main


func main() {


    str_1 := “Welcome to”


    str_2 := “Linuxhint”

    full_string := str_1 str_2

}

The example above uses the addition operator to combine two strings. We can then store the result into the variable.

You can also perform string concatenation using any of the print methods from the fmt package. Consider the example below:

fmt.Println(“Welcome to” “Linuxhint”)

The function takes the two string literals and combines them to a single string.

You can also pass two or more strings to any print method from the fmt package without using the addition operator. The functions will combine the strings and add a space between them.

Take a look at the example below:

fmt.Println(“Welcome to”, “Linuxhint.”)

This example passes the string to concatenate as parameters separated by a comma. The resulting string is as shown:

Strings.Join() Method

We can also use the join() method from the strings package to perform string concatenation. The function syntax is as shown:

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

The function takes two arguments. The first is a slice of string types and the second is a string describing the separator.

The function then returns a single string concatenated from the values of the slice.

Think the example shown below:

package main


import (


    “fmt”


    “strings”

)


func main() {


    str := []string{“Welcome”, “to”, “Linuxhint”}


    concat_str := strings.Join(str, ” “)


    fmt.Println(concat_str)

}

The function takes the string slice elements and combines them to a single string using a space as the separator.

The resulting value is as:

String Formatting – Sprintf

We can also use string formatting patterns provided by the Sprintf, Sprint, and Sprintln methods from the fmt package.

The following example illustrates how to perform string concatenation using the Sprintf() method.

str_1 := “Hello”


str_2 := “World”


concated_string := fmt.Sprintf(“%s%s”, str_1, str_2)


fmt.Println(concated_string)

Here, we use the %s formatter to concatenate two strings using the Sprintf() method.

Byes.Buffer() Method

Golang contains a bytes package that provides a buffer type, a variable-sized buffer of bytes with Read/Write methods.

We can write strings using the WriteString method, allowing us to transform them into a string.

An example is as shown:

package main


import (


    “bytes”


    “fmt”

)


func main() {


    str_1 := “Hello”


    str_2 := “World”


    var concat bytes.Buffer


    concat.WriteString(str_1)


    concat.WriteString(str_2)


    fmt.Println(concat.String())

}

The WriteString() method appends the string to the bytes.buffer type.

Strings.Builder() Method

The strings package also provides us with a Builder type that allows us to build strings. It has a WriteString method similar to the ones provided by the bytes package.

Consider the example:

package main

import (


    “fmt”


    “strings”

)

func main() {


    str_1 := “Hello”


    str_2 := “World”

    var concat strings.Builder

    concat.WriteString(str_1)


    concat.WriteString(str_2)

    fmt.Println(concat.String())

}

Summary

This guide covered various techniques to perform string concatenation in Go. Using the plus operator, you can add two or more strings together. You can also opt for other methods, including the join method, Sprintf, buffer, and more.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/john-150×150.png621352ad1e25e.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list