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

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

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


当前回答

根据语言规范,可以使用原始字符串文本,其中字符串由反引号而不是双引号分隔。

`line 1
line 2
line 3`

其他回答

根据语言规范,可以使用原始字符串文本,其中字符串由反引号而不是双引号分隔。

`line 1
line 2
line 3`

在go中,你必须非常小心格式和行距,一切都很重要,这里有一个工作示例,请尝试一下https://play.golang.org/p/c0zeXKYlmF

package main

import "fmt"

func main() {
    testLine := `This is a test line 1
This is a test line 2`
    fmt.Println(testLine)
}

可以使用原始文字。实例

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)
}

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

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

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

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

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.
             `