How to Concatenate Strings in GoLang?

Learn via video courses
Topics Covered

Overview

The strings are immutable in the Go language and are an arbitrary chain of bytes that are encoded with UTF-8. The process of adding or joining two or more strings together to make a new string is termed concatenation.

Introduction

The string is a slice of bytes enclosed in “”. These can be created by enclosing their contents in “”. The strings in the Go language are immutable which means once a string is created it’s not possible to change it.

Using the Plus (+) Operator

The + operator performs golang string concatenation and += also performs concatenation.

To understand how to use + operator for Golang Concat Strings, let’s take an example.

Output:

Explanation: In this example, a string s is initialized to an empty string and is printed. The string s1 is initialized to Scaler and string s2 is initialized to Academy. Both the strings are joined or concatenated by using the + operator and are initialized to s3. The variable s3 is then printed at the output.

Using String Append

The string can be appended to another string y using the += operator just like we did in the above code. This is, however, a slightly shorter way of concatenating a string. The append function usually happens on the right side of the string that is to be appended. The example given below shows how to concatenate two strings using the append method.

Output:

Explanation: In this example, two strings are assigned, the first is u which is assigned the word Scaler and the second string is v which is assigned from Scaler. The string assigned to the variable v is appended to the string of variable u by using the += operator. This sets the string in variable u to, Hello from Scaler which is displayed in the output.

Using Join()

The strings join function in the Go language is used for concatenating two strings. The signature method of the strings join function is as shown below:

The function takes two arguments, the first one is a slice of strings and the second one is a separator that is used for joining them. The resultant string at the output is a single string. Let’s take an example to understand this better.

Output:

Explanation: In this example, the list of strings is joined together by using the join function in the strings package. The output is then printed which joins the individual string words together in a sentence.

Using Sprintf()

The Sprintf() is a method fmt package and can also be used for golang string concatenation. It is one of the most simple ways of concatenating strings. Let’s take an example to understand this better. package main

Output:

Explanation: In this example, two variables are initialized with respective strings, the s1 is initialized to the string hello whereas the string s2 is initialized to the string scaler. The variable v is initialized with the function that concatenates the string s1 and s2 and prints helloscaler at the output.

Using the Bytes Buffer Method

The byte buffer is contained in package bytes containing a typed buffer. The WriteString method can be used to write a string and then transforms that into a string which is an efficient way of golang string concatenation. To understand this better, let’s take an example.

Output:

Explanation: In this example, the variable b is used as a byte buffer. This variable is then used for invoking the WriteString function from the bytes package which writes the string in a continuous manner. This means that the strings get appended to the previous string as long as the function is mentioned.

Go Strings Builder Method

The strings package has a building type which is an efficient way of building a string. This method uses less memory to concatenate two strings, making it a better way to concatenate two strings. This function allows concatenating two strings in a faster and in an efficient way. To understand this better, let’s take an example given below.

Output:

Explanation: In this example, the method from the string builder, WriteString is used for concatenating two strings. It works similarly to the previous example wherein it appends the string to the previous string. The reason for using string builder is that it is faster and more efficient than the previous WriteString method.

Using the Repeat() Method to Concatenate the Same String Multiple Times

This method is used when we have to join the same string multiple times to form another string. The strings package is used as it does this in an efficient way. To understand this better, let’s look at the example given below.

Output:

Explanation: In this example, the method Repeat is used which resides in the strings package. This function repeats the string as many times as mentioned in the argument of the method. The output is the concatenation of the same string.

Conclusion

  • The strings are immutable in the Go language and are an arbitrary chain of bytes that are encoded with UTF-8. The process of adding or joining two or more strings together to make a new string is termed concatenation.
  • The string is a slice of bytes enclosed in “”. These can be created by enclosing their contents in “”. The strings in the Go language are immutable which means once a string is created it’s not possible to change it.