What concatenation is
Concatenation means joining strings together to make a longer one. In Go the + operator does this for strings and builds a new string:
message := "Hello, " + "Andy"
fmt.Println(message) // Hello, Andy
Notice that Go inserts nothing between the pieces. The space after the comma has to be part of one of the strings.
Growing a string with +=
When you want to add text to a string variable, += is the shorthand. message += adjective means message = message + adjective:
message := "Coding is "
adjective := "worthwhile"
message += adjective
message += ", isn't it?"
fmt.Println(message) // Coding is worthwhile, isn't it?
The variable message is reassigned each time. The string type in Go is immutable, which means an existing string is never changed in place. Each += builds a new string and stores it in message, and the old value is simply no longer used. For a handful of joins that cost is invisible.
Joining several pieces at once
A chain of + operators joins as many pieces as you like, from left to right. A common use is putting a name together from parts, with a literal space as one of the pieces:
first, last := "Andy", "Smith"
full := first + " " + last
fmt.Println(full) // Andy Smith
The length of the result is the sum of the lengths of the parts, so len(full) is 10 here. The order matters: "x" + "y" gives xy and "y" + "x" gives yx. Also, a string variable that has been declared without a value is the empty string, so appending to it with += works from the start.
Strings cannot be edited in place
Joining is the way to make a changed copy, because a Go string can't be modified through its index. Writing s[0] = 'G' on a string s is a compile error: you cannot assign to s[0]. To change a string you build a new one, for example by joining pieces of the old one.
The lesson's exercise
The starter code declares message and adjective. The exercise has three steps:
- Append the variable.
message += adjectiveturns"Coding is "into"Coding is worthwhile". - Append a literal.
message += ", isn't it?"adds the ending. The apostrophe is fine inside a double-quoted string. - Print the result.
fmt.Println(message)shows the finished sentence.
message := "Coding is "
adjective := "worthwhile"
message += adjective
message += ", isn't it?"
fmt.Println(message)
The output is Coding is worthwhile, isn't it?. The space after is comes from the original "Coding is ", and the comma has no space before it.
Things to watch for
- Both sides must be strings. Go does not convert numbers automatically. With
n := 5, the expression"n = " + nis rejected with an error about mismatched typesuntyped string and int. Convert first, withstrconv.Itoa(n)orfmt.Sprint(n); joined with"n = ", either givesn = 5. - Spaces are yours to manage. Forgetting the space gives
Coding isworthwhile. - Using
+=on something undeclared. The variable has to exist first.message +=on an unknown name is a compile error. - The order matters.
a + bandb + aare different strings. - Unused variables. Go refuses to compile if you declare
adjectiveand never use it. The exercise uses it in the first step.
Reading the exercise's output
It is worth predicting the output before you run the code. After the first step, message holds Coding is worthwhile, with no punctuation yet. After the second step, the comma, a space and the words isn't it? are added, and the whole sentence is ready to print. Printing before the second step would show the unfinished sentence, which is why the print comes last in the exercise.
Many joins in a loop
Because each += copies the text built so far, joining thousands of pieces in a loop does a lot of copying. For that, the standard library offers strings.Builder, with its WriteString method, and strings.Join for a slice of pieces. For the short messages in this lesson, + and += are the right and simplest tool.
Key takeaways
+joins two strings into a new one, and+=appends to a string variable.- Go adds no separator, so include spaces yourself.
- Strings are immutable, so every join creates a new string.
- Only strings can be joined with
+. Convert numbers first.