7 Ways to Perform String Concatenation in Go

7 Ways to Perform String Concatenation in Go's picture

If you wish to achieve proficiency in Golang development, then you ought to be familiar with strings. If you are coming from a Python, C++ or Java background, then you will find strings to be a bit different in Golang. Each character in strings in Golang is signified by multiple bytes employing the UTF-8 Encoding. But that’s a different story.

In this blog, we will delve into concatenation of a string and how you can build a string. So, let’s start as we have a lot to cover.

String Building and Concatenation in Golang

So what is concatenation? In simple words, concatenation refers to the process of combining/adding two or multiple strings to form a single string. Thus, in a way, you are building new strings as well. Now, there are several ways to concatenate and build a string.

1. Concatenation Operator (Plus Operator)

  • Now this is a very basic method to concatenate a string. To understand this, let us code. First, we will create a new directory named ‘Strings’ inside go-workspace.
Concatenation Operator (Plus Operator)
  • With this, our IDE will open up (VS Code in this case). And we type
1 2 3 4 5 6 7 8 9 10 11 12 package main import “fmt” func main() { var s1 string s1= “First” var s2 string s2= “Concatenation” s3 :=”Method” fmt.Println (“Result:”, s1+ “ “+ s2+ “ “+ s3) }
  • You will notice that for creating and initializing s1 and s2, we use the ‘var’ keyword. And for s3, we are using the shorthand declaration. This is just to showcase the different ways you can initialize a string.
  • The output that we get is shown below:
String Concatenation in Go

Thus, you get a new string “Result: First Concatenation Method”

2. String Append

  • In the String Append method, we will use the ‘+=’ operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package main import "fmt" func main() { s1 := "Second" s2 := " Concatenation" s1 += s2 fmt.Println("String: ", s1) s1 += " Method" fmt.Println("String: ", s1) }

Here, we initialize two string variables s1 and s2. Then we perform the append method ‘s1 += s2’. Then we add “ Method” to the newly formed (concatenated) s1. This will give me the output “String: Second Concatenation Method”. Let’s see the output now.

String Second Concatenation Method

3. Strings Join() Function

  • This is another simple method to concatenate a string in Golang. What this function will do is that it will combine different elements in the slice of a string to create a new string. Let’s see how it works.
1 2 3 4 5 6 7 8 9 10 11 package main import ( “fmt” “strings” ) func main() { s := [] string {“Third”, “ Concatenation”, “ Method”} t := strings.Join (s, “”) fmt.Println (t) }
  • An important point to notice here is that in order to use strings.Join(), we have to import the package strings.
  • We create a slice s of type string and we include elements “Third”, “ Concatenation”, “ Method”. Then we use the Join() function in the variable t asking Golang to join the elements in s.
  • Let’s see the output
Strings Join() Function output

4. Sprintf() Method

This is a widely used method to concatenate two or more strings. Let’s see how it works

1 2 3 4 5 6 7 8 9 10 11 12 13 14 package main import "fmt" func main() { s1 := "Fourth" s2 := " Concatenation" s3 := " Method" output := fmt.Sprintf("%s%s%s", s1, s2, s3) fmt.Println(output)
  • The program is self-explanatory. We initialize three strings s1, s2 and s3. In another variable ‘output’ we use the Sprintf() method to concatenate the strings. Finally, we print the output.
Sprintf() Method output

5. Bytes Buffer Method

  • The fifth method is the Bytes Buffer Method. To use bytes.buffer, we have to import the bytes package. And we also have to take the help of a method called WriteString().
  • You will notice that there is no unnecessary generation of string objects. So, let us type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package main import ( “fmt” “bytes” ) func main() { var s1 bytes.Buffer s1.WriteString(“Fifth”) s1.WriteString(“ Concatenation”) s1.WriteString(“ Method”) fmt.Println (s1.String()) }
  • Let us check the output
Bytes Buffer Method output

This method will come in handy when you will devote yourself fully to Golang development. And if you come across anything intricate, (that you can’t solve by yourself) you can take the help of professional experts for the smooth execution of your project.

6. Repeat Method

  • As the name suggests, with this method, we can amalgamate the same string a specified number of times to form a different string.
  • What you have to remember is that in order to use this method, we have to import the strings package. So, let us type the code now.
1 2 3 4 5 6 7 8 9 package main import ( “strings” “fmt” ) func main() { fmt.Println(strings.Repeat (“Sixth Concatenation Method”, 3)) }
  • As per the code, the new string should repeat “Sixth Concatenation Method” three times for a new string. This is confirmed by the output below.
Repeat Method output

7. Strings Builder Method

  • You get to use the type builder with the package strings. This is regarded as an efficient way to build strings. Why? This is simply because it uses less memory to concatenate strings.
  • This is used in conjunction with the function WriteString, which enables the code to run faster. Here is an example below:
1 2 3 4 5 6 7 8 9 10 11 12 package main import ( “fmt” “strings” ) func main() { var s1 strings.Builder s1.WriteString (“Seventh”) s1.WriteString (“ Concatenation”) s1.WriteString (“ Method”) fmt.Println (s1.String()) }
  • Now, let us check the output.
Strings Builder Method output

  • We can use the builder type to add bytes or runes to the string.

(Side Note: The byte data type refers to ASCII characters. On the contrary, the runes refers to a wide array of Unicode characters as per the encoding in the UTF-8 format. Basically, a rune represents a Unicode code point which is a numerical value that in turn represents a Unicode character)

  • Using Strings Builder to Create a String from Byte Slices

From the name itself, you can decipher that we will create byte slices and build a string. Here we will deal with ASCII codes.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package main import ( “fmt” “strings” ) func main() { string :=strings.Builder{} a := []byte {66, 117, 105, 108, 100} b := []byte {32} m := []byte {97} d := []byte {32} e := []byte {115, 116, 114, 105, 110, 103} string.Write (a) string.Write (b) string.Write (m) string.Write (d) string.Write (e) fmt.Println(string.String()) }
  • If we check the output, we get
Using Strings Builder to Create a String from Byte Slices

Thus, you have to be familiar with the ASCII or look for them online.

So, there you have it! The various ways of concatenating and building strings as a result. Now which one should you use? Well, it depends on the program requirement and the complexity. If you want faster processing, then you should go ahead with Strings Builder. Otherwise, if you are working on a simple program, then the Append or Join() function might work well for you.


Build Your Golang Team