2 ways to write multiline strings

yourbasic.org/golang
Amiga style ASCII art

Raw string literals

Raw string literals, delimited by back quotes, can contain line breaks.

str := `First line
Second line`
fmt.Println(str)
First line
Second line

Raw strings literals are interpreted literally and backslashes have no special meaning.

Interpreted string literals

To insert escape characters, use interpreted string literals delimited by double quotes.

str := "\tFirst line\n" +
"Second line"
fmt.Println(str)
   First line
Second line

The escape character \t denotes a horizontal tab and \n is a line feed or newline.

The runes and character encoding article has a complete list of escape characters and special values that can be used in interpreted string literals.

Further reading

String functions cheat sheet

Share this page: