Go Programming
Multiline Strings
Coding Techniques
Programming Tutorials
Go Language Syntax

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.

Browse interview questions

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.

go
1package main
2
3import "fmt"
4
5func main() {
6    text := `line one
7line two
8line three`
9    fmt.Println(text)
10}

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:

go
query := `SELECT id, name
FROM users
WHERE active = true`

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.

go
1package main
2
3import "fmt"
4
5func main() {
6    text := `first line\nsecond line`
7    fmt.Println(text)
8}

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.

go
1package main
2
3import "fmt"
4
5func main() {
6    text := "line one\nline two\nline three"
7    fmt.Println(text)
8}

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:

go
1package main
2
3import "fmt"
4
5func main() {
6    name := "Ada"
7    message := fmt.Sprintf("Hello, %s.\nWelcome to Go.\nEnjoy your stay.", name)
8    fmt.Println(message)
9}

You can also use a raw string with actual newlines if that reads better:

go
1package main
2
3import "fmt"
4
5func main() {
6    name := "Ada"
7    message := fmt.Sprintf(`Hello, %s.
8Welcome to Go.
9Enjoy your stay.`, name)
10    fmt.Println(message)
11}

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.

go
text := "Use the `go test` command"

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.

go
text := `first line
    second line with spaces`

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.Sprintf when 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 \n or \t.
  • Use fmt.Sprintf when variables must be inserted into multiline output.
  • Watch out for preserved indentation and the fact that raw strings cannot contain backticks directly.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions