Clive B.
—You don’t know how to combine multiple strings (for example, "42"
and "Example Avenue"
) into a single string ("42 Example Avenue"
.).
Depending on your requirements, you can concatenate strings by:
+
operatorfmt.Sprintf
strings.Builder
+
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".
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". }
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". }
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.
fmt
package documentationstrings
package documentationTasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.