Go是否有类似于Python的多行字符串:

"""line 1
line 2
line 3"""

如果没有,那么编写跨多行字符串的首选方式是什么?


当前回答

可以使用原始文字。实例

s:=`stack
overflow`

其他回答

对我来说,如果添加不成问题,这就是我使用的方法。

fmt.Sprintf("Hello World\nHow are you doing today\nHope all is well with your go\nAnd code")

否则,可以使用原始字符串

multiline := `Hello Brothers and sisters of the Code
              The grail needs us.
             `

可以使用原始文字。实例

s:=`stack
overflow`

Go和多行字符串

使用反引号可以有多行字符串:

package main

import "fmt"

func main() {

    message := `This is a 
Multi-line Text String
Because it uses the raw-string back ticks 
instead of quotes.
`

    fmt.Printf("%s", message)
}

不要使用双引号(“)或单引号符号('),而是使用反引号来定义字符串的开始和结束。然后可以将其换行。

如果您缩进字符串,请记住,空格将计数

请检查操场并用它做实验。

你可以在内容周围加上“”,比如

var hi = `I am here,
hello,
`

你可以写:

"line 1" +
"line 2" +
"line 3"

其与:

"line 1line 2line 3"

与使用反引号不同,它将保留转义字符。请注意,“+”必须在“前导”行上-例如,以下内容将生成错误:

"line 1"
+"line 2"