How to properly write multiline strings in Go?
Hi everyone! I just started learning Go after Python and encountered a problem when working with long strings. In Python, I'm used to using triple quotes for multiline strings, for example:
```python
text = """This is a multiline
string in Python,
it preserves line breaks."""
```
But in Go, I can't find an equivalent. I tried to write it like this:
```go
package main
import "fmt"
func main() {
text := "This is a string\ncontinues on a new line"
fmt.Println(text)
}
```
And got a compilation error: `invalid syntax: unexpected newline in string literal`. How do I properly declare a string that spans multiple lines in code? Do I need to use any special characters or functions? I'm especially interested in cases where the string contains HTML, SQL queries, or JSON — it's inconvenient to write th
No answers yet.
Log in to leave an answer
Blogs