Different ways to concatenate two strings in Golang
Last Updated :
28 Oct, 2024
In Go, strings are immutable sequences of bytes encoded with UTF-8. Concatenating two or more strings into a single string is straightforward in Go, and there are several ways to accomplish it. In this article,we will learn various ways to concatenate two strings in Golang.
Example
Input:
s1 := "Hello, "
s2 := "Geeks!"
Output:
"Hello,Geeks!"
Syntax
s1 + s2 #Using the "+" Operator
var b bytes.Buffer
b.WriteString(s1) # Using bytes.Buffer and WriteString()
b.WriteString(s2)
fmt.Sprintf("%s%s", s1, s2) # Using fmt.Sprintf
s1 += s2 # Using the += Operator (String Append)
strings.Join([]string{s1, s2}, "") # Using strings.Join
var builder strings.Builder
builder.WriteString(s1) # Using strings.Builder and WriteString()
builder.WriteString(s2)
Using the "+" Operator
The + operator is the simplest way to concatenate strings in Go. This operator combines two or more strings.
Syntax
s1 + s2
Example:
Go
package main
import "fmt"
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Concatenating using + operator
result := s1 + s2
fmt.Println("", result)
}
Using bytes.Buffer and WriteString()
The bytes.Buffer approach allows efficient string concatenation without generating intermediate strings, using WriteString() to append each segment.
Syntax
var b bytes.Buffer
b.WriteString(s1)
b.WriteString(s2)
Example:
Go
package main
import (
"bytes"
"fmt"
)
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Initializing a bytes buffer
var b bytes.Buffer
b.WriteString(s1)
b.WriteString(s2)
fmt.Println("", b.String())
}
Using fmt.Sprintf
The fmt.Sprintf function offers a formatted approach to string concatenation.
Syntax:
fmt.Sprintf("%s%s", s1, s2)Example:
Go
package main
import "fmt"
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Concatenating using fmt.Sprintf
result := fmt.Sprintf("%s%s", s1, s2)
fmt.Println("", result)
}
Using the += Operator (String Append)
In Go, you can append to an existing string using the += operator. This operation adds the second string to the end of the first.
Syntax
s1 += s2
Example
Go
package main
import "fmt"
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Concatenating using += operator
s1 += s2
fmt.Println("", s1)
}
Using strings.Join
The strings.Join function can concatenate elements from a slice of strings with a specified separator. While it is most useful for multiple strings, it works well for pairs as well.
Syntax
strings.Join([]string{s1, s2}, "")Example:
Go
package main
import (
"fmt"
"strings"
)
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Concatenating using strings.Join
result := strings.Join([]string{s1, s2}, "")
fmt.Println("", result)
}
Using strings.Builder and WriteString()
The strings.Builder type provides a similar approach to bytes.Buffer for efficient string concatenation with WriteString().
Syntax
var builder strings.Builder
builder.WriteString(s1)
builder.WriteString(s2)
Example
Go
package main
import (
"fmt"
"strings"
)
func main() {
s1 := "Hello, "
s2 := "Geeks!"
// Initializing a strings builder
var builder strings.Builder
builder.WriteString(s1)
builder.WriteString(s2)
fmt.Println("", builder.String())
}
Note:
The error undefined: strings.Builder will occur if your Go version is earlier than 1.10 because the strings.Builder type was introduced in Go 1.10. If your Go environment is set to a version earlier than 1.10, strings.Builder will be undefined. To resolve this issue, make sure that your Go version is 1.10 or higher.
Explore
Overview
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays
Slices
Strings
Pointers
Concurrency