Multiline Strings in Go

If you’re coming from the world of Python, you might be used to using triple double quotes for multi line strings, and wonder how to use multi line strings in Go.

In Python:

multiline = """string
with
four
lines"""

Go has a similar feature, using back ticks:

multiline := `string
with
four
lines`

Note that if you want to include a ` character, you’ll need to concatenate using "`".

multiline := `this has
a ` + "`" + ` back tick
character`

Also, escaped characters (e.g. \n or \t) won’t be unescaped. It would be pretty rare to need to use them, as you can write a newline, tab, or any unicode character directly into the string. If required, though, use the same technique as above.

That’s all folks!

 

Leave a Reply

Your email address will not be published. Required fields are marked *