How to Write Multiline Strings in Golang

To write multiline strings in Golang using backticks (`). A backtick(backquote) creates a literal raw string, which can span multiple lines and include special characters without escaping them.

Raw string literals

package main

import "fmt"

func main() {
  str := `This is a
           multiline string
           in Golang`
 
  fmt.Println(str)
}

Output

This is a
multiline string
in Golang

Any spacing you use in the string to retain indentation will also be present in the final string.

Raw string literals are character sequences between backticks. Within the quotes, any character may appear except the backtick.

Raw string literals are uninterpreted and do not process any escape sequences. If you need to escape other characters, you should use an interpreted string literal, which is enclosed by double quotes (“) and supports escape sequences that start with a backslash (\).

That’s it.

Leave a Comment