All lessons

Working with Text

7 min · Go

Join strings, measure them, and format text with fmt.

Go · Working with Text0/5 cleared

Enemy 1 of 5

Strings and concatenation

This code joins two pieces of text with + and prints the result as one greeting. Press Run to see it.

Why this works

A Go string is technically a read-only sequence of bytes, most often holding UTF-8 encoded text, but for everyday use, you can think of it the same way you think of strings in any other language: a piece of text in quotes.

Two strings are joined together with the same + operator used for adding numbers; Go determines from the operand types whether + means numeric addition or string concatenation. Because Go strings are immutable (they can't be changed in place), greeting + name always produces a brand-new string rather than modifying greeting itself.

You
Packet Wolf
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.