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.
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
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)
}
Thus, you get a new string “Result: First Concatenation Method”
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.
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)
}
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)
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())
}
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.
1
2
3
4
5
6
7
8
9
package main
import (
“strings”
“fmt”
)
func main() {
fmt.Println(strings.Repeat (“Sixth Concatenation Method”, 3))
}
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())
}
(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)
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())
}
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.