How do you write multiline strings in Go?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Go, the normal way to write a multiline string is with a raw string literal using backticks. That form preserves line breaks exactly as written. When you need escape sequences or formatted substitution, a regular double-quoted string is still often the better tool.
Raw String Literals
A raw string literal is enclosed in backticks.
In the source file above, the actual string contains real line breaks between the three lines. Go preserves those line breaks exactly.
Raw strings are great when you want:
- readable multiline text
- SQL or HTML snippets embedded directly in code
- JSON examples in tests
- backslashes without repeated escaping
A SQL example looks much cleaner as a raw string than as a chain of escaped fragments:
Because raw strings preserve characters literally, this is easy to read and easy to maintain.
What Raw Strings Do Not Do
Raw strings do not interpret escape sequences. The characters \n inside a raw string are just a backslash followed by n.
That prints the literal characters \n, not a newline.
If you want newline escapes, tab escapes, or quote escapes to be interpreted, use a double-quoted string instead.
Interpreted Strings with Escapes
Double-quoted strings are called interpreted string literals. They process escape sequences such as \n and \t.
This produces the same output as the raw multiline example, but the newlines are created with escapes rather than physical line breaks in the source.
This style is useful when:
- you need to control exact escape behavior
- you are formatting a shorter string
- you want to combine text with
fmt.Sprintf
Multiline Strings with Variables
Go does not have template literals with embedded expressions. When you need variables inside multiline output, fmt.Sprintf is the usual approach.
A simple interpreted-string example is:
You can also use a raw string with actual newlines if that reads better:
In that second example, the source includes real line breaks between lines. That is often the cleanest option when the output block is larger.
Backticks Are the Main Limitation
Raw strings cannot contain a backtick directly, because the backtick ends the literal.
If your content includes backticks, use a double-quoted string or concatenate pieces.
That is simpler than trying to force a raw string into a case it does not represent well.
Indentation and Whitespace Matter
Raw multiline strings preserve indentation too. If the literal is indented in your source code, those spaces become part of the string.
That behavior is useful for some generated output and test fixtures, but it can also surprise you if extra spaces sneak in from code formatting.
When exact output matters, always inspect the resulting string carefully.
Choosing the Right Form
A practical rule of thumb is:
- use raw strings for readable literal blocks of text
- use interpreted strings for escape-heavy content
- use
fmt.Sprintfwhen variables must be inserted
This is not about one syntax being universally better. It is about choosing the one that matches the string’s job.
Common Pitfalls
The biggest pitfall is expecting raw string literals to interpret \n or \t. They do not.
Another issue is forgetting that indentation is preserved, which can create unwanted spaces in output.
Developers also often choose raw strings and then realize the content contains backticks, forcing a switch to another style.
Finally, if you need formatting with variables, do not expect raw strings alone to handle substitution. Pair them with fmt.Sprintf or use an interpreted format string.
Summary
- Use backtick raw string literals for the simplest multiline strings in Go.
- Raw strings preserve line breaks and most characters exactly as written.
- Use double-quoted strings when you need escape sequences such as
\nor\t. - Use
fmt.Sprintfwhen variables must be inserted into multiline output. - Watch out for preserved indentation and the fact that raw strings cannot contain backticks directly.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.