Quick tutorial on how to create multiline strings in Golang.
Normally in Go, we’ll create a string with double quotes:
myString := "this is a string"
To have a string span multiple lines, we could use \n
like so:
uglyMultilineString := "This is line one.\nThis is line two.\nThis is line three."
While this does work, it’s really ugly. It also is a nightmare if you have to copy and paste a multiline string that doesn’t have the \n
’s already included.
Luckily, Go has a better solution.
Go made it really easy to create a multiline string. To create a multiline string, simply use backticks instead of quotes:
multiLineString := `
This is line one.
This is line two.
This is line three.
`
fmt.Println(multiLineString)
// output:
// This is line one.
// This is line two.
// This is line three.
For other Golang examples: https://www.mikesallese.me/tags/golang