How do we write multiline strings in other programming language?
In Golang, writing a multiline string is really simple. This article will demonstrate how to do that.
In many programming languages, we can use multiline strings. For example in Python:
"""this is line 1
and line 2
and line 3"""
or in Java:
public String textConcat() {
return """
It is because
you made yourself
and easy option.
...""";
}
Does Go have something similar to write multiline string? Yes, let's check the different possible ways to achieve this.
Method 1: Using raw string literal (backtick)
According to the language specification, you can use a raw string literal
, where the string is delimited by backticks instead of double quotes.
Raw string literals are character sequences between back quotes, as in `bar`. Within the quotes, any character may appear except the back quote.
`this is line 1
and line 2
and line 3`
A significant part is that is raw literal not just multi-line and to be multi-line is not the only purpose of it.
The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines. Carriage return characters ('\r') inside raw string literals are discarded from the raw string value.
The 'raw quote' as it is called, does not parse any kind of special characters including escape sequences. So if you have a requirement to include double quotes, single quotes, escape characters or new lines into your string then you can choose raw string literal. It keeps the patterns clean and relatively readable.
package main
import "fmt"
func main() {
// Declare golang multiline string
multiLine := `line 1 \n \t number
"and line 2"
and line 3`
fmt.Println(multiLine)
}
As you can see in the output, all our special characters are considered as string and printed on the output:
$ go run main.go
line 1 \n \t number
"and line 2"
and line 3
Method 2: Using string concatenation
Possibly you have long line which you want to break and you don't need new lines characters in it. In this case you could use string concatenation.
Example-1: Concatenate strings with newline escape character
package main
import "fmt"
func main() {
// Declare golang multiline string
multiLine := "line one " +
"by line two " +
"and line three " +
"and line four"
fmt.Println(multiLine) // No new lines will be printed
}
Output:
line one by line two and line three and line four
Different from method 1, with this method, we can print escape characters as the code shown below:
multiLine := "line one \n" +
"by line two \n" +
"and line three \n" +
"and line four"
fmt.Println(multiLine) // New lines will be interpreted \n and shown on the screen
Output:
$ go run main.go
line one
by line two
and line three
and line four
Example-2: Declare multiline strings with variable
We modify our existing example and also add one variable to the multiline string definition inside the main function.
package main
import "fmt"
func main() {
// Declare variable
car := "line two from variable"
// Declare golang multiline string
multiLine := "line one \n" +
car + "\n" +
"by line three \n" +
"and line four \n" +
"and line five"
fmt.Println(multiLine) // New lines will be interpreted \n and shown on the screen
}
Here in the output you can observe that our variable has been successfully printed along with multiline string
$ go run main.go line one line two from variable by line three and line four and line five
Summary
In this article, I have shown 2 ways to write multiline strings in Go: using raw string literal
and string concatenation
. Noted that when using raw string literal: be very careful on formatting and line spacing, everything counts. For example, if put a space at the end of line, it will be invisible in editor but present in the string.
References
https://go.dev/ref/spec#String_literals