Golang multiline string: raw literals, concatenation, and variables

Golang multiline string and go multiline string: raw string literals in backticks, golang multiline string literal rules, concatenation with \\n, golang multiline string with variables via fmt and strings.Builder.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang multiline string: raw literals, concatenation, and variables

Searches for golang multiline string, go multiline string, multiline string golang, golang multiline strings, golang multi line string, multiline string go, go multi line string, golang multiline string literal, multiline string in golang, or golang multiline string with variables all map to the same language rules: Go has raw string literals (backticks) that can span lines without escape processing, and interpreted string literals (double quotes) where you use \n and break source lines with concatenation or tools like fmt.Sprintf and strings.Builder. Other languages allow multi-line "..." text blocks; Go does not—use backticks or +. For Python-style triple quotes see multiline comments / strings in Python.

Tested with Go 1.24 on Linux.


golang multiline string literal: raw strings (backticks)

The Go spec — String literals defines raw string literals: text between backticks `...`. Inside them, backslashes are not escapes (so \n is a backslash and n), and newlines are real newlines in the string value. The only character that cannot appear inside is a backtick itself (close the literal, use concatenation, or use an interpreted string).

go
package main

import "fmt"

func main() {
	s := `line 1 \n \t literal
"quotes are fine"
line 3`
	fmt.Print(s)
}
text
line 1 \n \t literal
"quotes are fine"
line 3

Carriage returns (\r) inside raw literals are dropped from the value (spec). Leading and trailing spaces on each physical line are kept—format carefully.


go multiline string with interpreted literals and +

Double-quoted strings cannot contain raw newline characters in source. Split long text across lines with +, and use \n, \t, \" where you need escapes:

go
package main

import "fmt"

func main() {
	s := "line one\n" +
		"line two\n" +
		"line three"
	fmt.Print(s)
}
text
line one
line two
line three

This is the usual go multi line string style when you need escape processing but readable source layout.


golang multiline string with variables

Backticks cannot interpolate identifiers. Common patterns:

Concatenation (simple):

go
name := "Ada"
body := "Hello " + name + ",\n" +
	"your build finished.\n"

fmt.Sprintf (formatting):

go
user, score := "Bob", 98
msg := fmt.Sprintf("%s scored %d\nnext line", user, score)

strings.Builder (many pieces, efficient):

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	name := "Ada"
	var b strings.Builder
	b.WriteString("# Report\n")
	b.WriteString("user: ")
	b.WriteString(name)
	b.WriteByte('\n')
	fmt.Print(b.String())
}

For many fixed lines stored as data, build a []string and use strings.Join(lines, "\n").


Choosing a style

Need Prefer
SQL, JSON templates, regex with lots of \ Raw `...`
Escapes like \n, \t, \" Interpreted "..." with +
Dynamic values +, Sprintf, or strings.Builder
HTML or mail bodies text/template or html/template (not shown here, but better than giant literals)

Summary

A golang multiline string is usually either a raw literal in backticks (golang multiline string literal with real newlines, no escapes) or a concatenated interpreted string for go multiline string style with \n. golang multiline string with variables requires concatenation, fmt.Sprintf, or a strings.Builder—not backtick interpolation. Watch invisible spaces at line ends in raw literals, and remember \r stripping in raw values.


References


Frequently Asked Questions

1. Can I put variables inside a backtick raw string in Go?

No. Raw string literals are constant; use concatenation, fmt.Sprintf, strings.Builder, or text/template for interpolation.

2. Why does \\n not become a newline inside backticks?

Backtick raw string literals do not process escape sequences; \n is two characters. Use an interpreted "string" with \n or put a real newline inside the backticks.

3. Can a normal double-quoted string span several lines in Go source?

No. Only raw literals can contain unescaped newlines. Break long interpreted strings across lines with + or a raw literal.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …