What concatenation is
Concatenating strings means joining them end to end to make a longer string. In Go you do it with the + operator, the same symbol that adds numbers. When both sides are strings, + joins them instead of adding.
message := "I love coding. " + "It is fun!"
fmt.Println(message)
I love coding. It is fun!
The result is a new string. The two original pieces are not changed.
Using variables
Concatenation is most useful when part of a message comes from a variable. You can mix string literals and string variables in one expression:
favoriteAnimal := "parrot"
message := "My favorite animal is the " + favoriteAnimal
fmt.Println(message)
My favorite animal is the parrot
The text inside quotes is written exactly as it should appear. The variable is outside the quotes, because you want its value, not the letters of its name. Change the variable and the message changes with it.
Chaining several pieces
You can use + as many times as you like in one expression. Go evaluates it from left to right, one join at a time: "a" + "b" + "c" gives abc.
Appending with +=
To add to a string you already have, use +=. It builds the longer string and stores it back in the variable:
s := "I love coding."
s += " It is fun!"
fmt.Println(s) // I love coding. It is fun!
Spaces are your job
+ joins exactly what it is given and adds nothing between the pieces. If the text needs a space, it must be inside one of the strings. Compare:
name := "John Doe"
language := "Python"
fmt.Println("My name is " + name + " and I code in " + language)
fmt.Println("My name is" + name + "and I code in" + language)
My name is John Doe and I code in Python
My name isJohn Doeand I code inPython
The second line forgot the spaces next to the quotes, so the words run together. A quick check is to look at each place where a quote meets a variable and ask whether a space belongs there.
Only strings can be joined to strings
Go does not convert numbers to text for you. This does not compile:
years := 5
fmt.Println("Years: " + years)
invalid operation: "Years: " + years (mismatched types untyped string and int)
Convert the number first with strconv.Itoa(years), which gives "5". Alternatively, give fmt.Println several arguments: it prints them separated by single spaces, so fmt.Println("Years:", years) prints Years: 5. That is not concatenation, though, and it inserts spaces you did not write.
Another way to build the same sentence
For messages with many pieces, fmt.Sprintf is often easier to read than a long chain of +. It takes a template with placeholders and returns the finished string. The placeholder %s stands for a string:
msg := fmt.Sprintf("My name is %s and I code in %s", name, language)
fmt.Println(msg) // My name is John Doe and I code in Python
This lesson practises +, which is the simplest tool and enough for short messages.
Strings don't change in place
A Go string is immutable. Joining never edits the original; it produces a new string. After t := s + "cd" with s holding "ab", s is still ab and t is abcd. Even s += "!" creates a new string and then stores it in s.
Common mistakes
- Putting a variable inside the quotes.
"My name is name"is just text, so the output contains the wordname, not the value. - Leaving text outside the quotes. Writing
My name iswithout quotes is a syntax error, because Go reads those words as separate names, not as one piece of text. - Forgetting a
+between two pieces. Two values side by side are a syntax error.
The exercise, step by step
The starter code declares two variables:
var name string = "John Doe"
var language string = "Python"
The task is to print one sentence that uses them, so it will still be correct if the values change.
- Start the call. Everything goes inside one
fmt.Println(...). - First literal.
"My name is ", with a space before the closing quote. - Add
nameafter a+, outside the quotes. - Second literal.
" and I code in "has a space at both ends: one separates it from the name, the other from the language. - Add
languagewith a final+.
var name string = "John Doe"
var language string = "Python"
fmt.Println("My name is " + name + " and I code in " + language)
It prints My name is John Doe and I code in Python. If you later set language = "Go", the same line prints ... and I code in Go without being edited.
Key takeaways
+joins two strings;+=appends to a string variable.- Variables go outside the quotes; the text you want to see goes inside.
- Put the spaces inside the string literals, because
+adds none. - A string and an
intcan't be joined with+: convert withstrconv.Itoafirst.