How to Write Multiline Strings in Golang

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

Raw string literals are strings surrounded by backticks instead of double quotes and can span multiple lines and include special characters without escaping them.

Example

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