Question
In Go, is there anything similar to Python's multiline string syntax?
"""line 1
line 2
line 3"""
If not, what is the preferred way to write strings that span multiple lines in Go?
Short Answer
By the end of this page, you will understand how Go represents multiline strings, when to use raw string literals versus interpreted string literals, how newlines are preserved, and what patterns developers commonly use in real Go programs.
Concept
In Go, multiline strings are usually written with raw string literals, which use backticks:
text := `line 1
line 2
line 3`
This is the closest equivalent to Python's triple-quoted strings for many use cases.
Go has two main kinds of string literals:
- Interpreted string literals: wrapped in double quotes (
"...") - Raw string literals: wrapped in backticks (
`...`)
Interpreted string literals
These process escape sequences such as:
\nfor newline\tfor tab\"for a quote
Example:
text := "line 1\nline 2\nline 3"
Raw string literals
These keep the text almost exactly as written, including line breaks.
Example:
text := `line 1
line 2
line 3`
This matters because in real programs, developers often need to store:
- SQL queries
- JSON samples
- HTML templates
- command output examples
- test data
- help text and CLI messages
Raw string literals make multiline content easier to read and maintain.
Mental Model
Think of Go strings as having two writing modes:
- Double quotes are like a text editor that understands special shortcuts. If you type
\n, Go turns it into a real newline. - Backticks are like a glass box around your text. Go keeps what you typed almost exactly as it appears.
So if you want to write a paragraph or block of text exactly as it looks on the screen, backticks are usually the simplest choice.
Syntax and Examples
Core syntax
Raw string literal
message := `line 1
line 2
line 3`
This creates a string containing actual line breaks.
Interpreted string literal
message := "line 1\nline 2\nline 3"
This also creates a multiline string, but the newlines are added using escape sequences.
Beginner-friendly examples
Example 1: Using backticks for multiline text
package main
import "fmt"
func main() {
text := `Hello,
This is a multiline string in Go.
Goodbye.`
fmt.Println(text)
}
Why this works
- The string starts and ends with backticks.
- The line breaks inside the string are preserved.
- This is usually the cleanest way to write readable multiline text.
Example 2: Using double quotes with \n
package main
import "fmt"
{
text :=
fmt.Println(text)
}
Step by Step Execution
Consider this example:
package main
import "fmt"
func main() {
message := `line 1
line 2`
fmt.Println(message)
}
Step by step
- Go sees the backtick at the start of the string.
- It treats everything until the closing backtick as raw text.
- The newline between
line 1andline 2becomes part of the string. - The variable
messagestores a string with two lines. fmt.Println(message)prints the string.fmt.Printlnalso adds its own newline after printing.
Output
line 1
line 2
Now compare it with this:
message := "line 1\nline 2"
What happens here
- Go sees double quotes, so this is an interpreted string.
- It reads
\nas a newline character. - The final stored string is still two lines.
Real World Use Cases
Multiline strings are useful in many real Go programs.
Common uses
SQL queries
query := `SELECT id, email
FROM customers
WHERE subscribed = true`
This keeps complex queries readable.
JSON examples or test payloads
payload := `{
"name": "Ava",
"active": true
}`
Or, if you want the formatting exactly as written:
payload := `{
"name": "Ava",
"active": true
}`
A more natural raw string version would be:
payload := `{
"name": "Ava",
"active": true
}`
In practice, developers often write actual formatted JSON with real newlines in a raw string.
CLI help messages
help := `Usage:
app serve
app version
Options:
--port Port number`
Templates and sample content
email := `Hello {{.Name}},
Your order has shipped.
Thanks!`
Test fixtures
Large expected outputs are often easier to compare when written as multiline strings.
Real Codebase Usage
In real codebases, developers usually choose between backticks and double quotes based on readability and needed features.
Common patterns
1. Raw strings for readable blocks of text
Used for:
- SQL
- HTML fragments
- sample API responses
- test fixtures
- documentation text
const helpText = `Usage:
tool run
tool test`
2. Double quotes when escapes are needed
If you need special characters or precise control, interpreted strings are often better.
path := "C:\\temp\\file.txt"
3. Concatenation for dynamic content
Static multiline text is often combined with variables.
name := "Sam"
message := "Hello, " + name + "\nWelcome back."
4. Formatting with fmt.Sprintf
Useful when a multiline string includes values.
report := fmt.Sprintf("User: %s\nStatus: %s", name, status)
5. Constants for repeated text
Large multiline text blocks are often stored as constants for reuse.
Common Mistakes
1. Expecting Python triple quotes in Go
Go does not use """...""" for strings.
Incorrect
text := """line 1
line 2"""
Correct
text := `line 1
line 2`
2. Forgetting that raw strings preserve formatting exactly
Extra spaces and indentation become part of the string.
text := `line 1
line 2`
That indentation is stored too.
3. Trying to use escape sequences inside raw strings
In a raw string, \n is usually treated as the characters backslash and n, not a special escape.
Broken expectation
text := `hello\nworld`
fmt.Println(text)
This prints:
hello\nworld
Use this instead
Comparisons
Raw strings vs interpreted strings in Go
| Feature | Raw string literal | Interpreted string literal |
|---|---|---|
| Delimiter | Backticks | Double quotes |
| Multiline support | Yes, naturally | Yes, using \n or line continuation through concatenation |
| Escape sequences processed | No | Yes |
| Best for readable blocks of text | Yes | Sometimes |
| Can easily include double quotes | Yes | Must escape them |
| Can include backticks directly | No | Yes |
Which should you choose?
| Situation | Better choice |
|---|
Cheat Sheet
Quick reference
Raw string literal
text := `line 1
line 2`
- Uses backticks
- Can span multiple lines
- Preserves line breaks
- Does not process most escape sequences
Interpreted string literal
text := "line 1\nline 2"
- Uses double quotes
- Escape sequences work
- Good when you need
\n,\t,\", or unicode escapes
Rules to remember
- Go does not use Python-style triple quotes.
- Backticks are the usual way to write multiline string literals.
- Raw strings keep spaces and indentation.
- Raw strings cannot directly contain backticks.
- Double-quoted strings are better when escape sequences are required.
Common patterns
const message = `Hello
World`
message := fmt.Sprintf("Name: %s\nStatus: %s", name, status)
Edge cases
FAQ
Is there a multiline string syntax in Go like Python triple quotes?
No. Go uses raw string literals with backticks instead of triple quotes.
What is the preferred way to write multiline strings in Go?
Usually, use a raw string literal with backticks because it is the most readable for multiline text.
Can Go strings span multiple lines with double quotes?
Yes, but usually by using \n escape sequences or concatenating strings. Raw strings are often cleaner for literal multiline text.
Do raw strings preserve indentation in Go?
Yes. Any spaces and tabs you include are stored in the string.
Can I use escape sequences like \n inside a raw string?
Not in the usual interpreted way. In a raw string, the characters are mostly kept as written.
Can a raw string contain backticks?
Not directly. If your content includes backticks, use a double-quoted string or build the string from parts.
Which is better for SQL queries in Go?
Raw strings are often better because they keep the query readable across multiple lines.
Mini Project
Description
Build a small Go program that prints a command-line help message using a multiline string. This demonstrates the most common beginner-friendly use of raw string literals: storing readable text exactly as it should appear to the user.
Goal
Create a Go program that stores and prints a formatted multiline help message using a raw string literal.
Requirements
- Define a multiline help message in a variable using backticks.
- Include at least a title, a usage section, and an options section.
- Print the message to the terminal with
fmt.Println. - Make sure the output formatting is easy to read.
Keep learning
Related questions
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.
Convert String to Integer in Go: Idiomatic Parsing with strconv.Atoi
Learn the idiomatic way to convert a string to an int in Go using strconv.Atoi, with examples, errors, and common mistakes.