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.
Similar Reads
Different ways to compare Strings in Golang In Go, strings are immutable sequences of bytes encoded in UTF-8. You can compare them using comparison operators or the strings.Compare function. In this article,we will learn different ways to compare Strings in Golang.Examplepackage main import ( "fmt" "strings" ) func main() { s1 := "Hello" s2 :
2 min read
Different Ways to Convert the Boolean Type in String in Golang In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : C
2 min read
Different Ways to Convert an Integer Variable to String in Golang Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function:Â The Itoa stands for Integer t
3 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read