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

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

其他回答

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

var hi = `I am here,
hello,
`

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

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中,你必须非常小心格式和行距,一切都很重要,这里有一个工作示例,请尝试一下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)
}

在Go中创建多行字符串实际上非常简单。在声明或分配字符串值时,只需使用backtick(`)字符。

package main

import (
    "fmt"
)

func main() {
    // String in multiple lines
    str := `This is a
multiline
string.`
    fmt.Println(str + "\n")
    
    // String in multiple lines with tab
    tabs := `This string
        will have
        tabs in it`
    fmt.Println(tabs)
}