Concatenating strings in Go

Clive B.
—The Problem
You don’t know how to combine multiple strings (for example, "42" and "Example Avenue") into a single string ("42 Example Avenue".).
The Solution
Depending on your requirements, you can concatenate strings by:
- Using the
+operator - Joining a slice of strings
- Using
fmt.Sprintf - Using
strings.Builder
Using the + Operator
+ OperatorThe simplest way to combine strings, especially when concatenating just a few values, is to use the + operator:
print("42" + " " + "Example Avenue") // Prints "42 Example Avenue".
Joining a Slice of Strings in Go
If you want to join your strings with a common separator or have too many strings to concatenate, you can use the strings.Join function:
package main import "strings" func main() { address := []string{"1 Sample Apartments", "42 Example Avenue", "Model Country"} print(strings.Join(address, ", ")) // Prints "1 Sample Apartments, 42 Example Avenue, Model Country". }
Concatenating Different Types Into Strings in Go
The Go fmt package offers simple, C-like string formatting, with various verbs (variable placeholders) that make it easier to work with different types. This is especially useful when joining multiple types into the same string.
However, the fmt.Sprintf function uses something called reflection to enable string formatting, which is convenient but doesn’t perform as well as direct type conversion.
package main import "fmt" func main() { var ( building string = "1 Sample Apartments" street string = "Example Avenue" number int = 42 ) // `%d` is for digit, and `%s` is for string. address := fmt.Sprintf("%s, %d %s", building, number, street) print(address) // Prints "1 Sample Apartments, 42 Example Avenue". }
High-Performance String Concatenation in Go
If you wish to avoid the performance drawbacks of using fmt.Sprintf, you can use the strings.Builder.
Although the + operator is faster than the fmt.Sprintf function, it isn’t the most efficient choice. Because strings are immutable in Go, every time + is called, Go allocates the entire resulting string to a new location in memory, which can waste time.
The Go strings.Builder minimizes memory copying, and has a nice API to boot.
package main import "strings" func main() { address := []string{"1 Sample Apartments", "42 Example Avenue", "Model Country"} var b strings.Builder for index, value := range address { // Prepend with a comma and a space if it's not the first item. if index > 0 { b.WriteString(", ") } b.WriteString(value) } print(b.String()) // Prints "1 Sample Apartments, 42 Example Avenue, Model Country". }
Interestingly, strings.Join uses a strings.Builder under the hood.
Further Reading
- The Go
fmtpackage documentation - The Go
stringspackage documentation - The Laws of Reflection Go blog post
- SentryGo Error Tracking and Performance Monitoring
- Syntax.fmListen to the Syntax Podcast
- Listen to the Syntax Podcast
![Syntax.fm logo]()
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODES
Considered “not bad” by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
