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

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

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


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

1
2
3
`line 1
line 2
line 3`
  • 作为旁注:调用的"原始引用"不解析转义序列。因此,编写正则表达式模式是字符串文字的首选,因为它们通常包含非标准转义序列,这会使Go编译器抱怨不进行双重转义。它使图案保持清洁并且相对可读。
  • 但是在使用endline空格时需要注意这一点。例如,如果在line 1之后放置一个空格,它将在编辑器中不可见但存在于字符串中。
  • 请注意,这对SQL来说很痛苦
  • @DanieleD那是一种轻微的不成熟,但是哪种方言?想必主要是MySQL? stackoverflow.com/a/10574031请注意,通过扩展相同的参数,嵌入markdown或shell脚本很困难(如果您选择使用反引号代替$(abcd))。
  • 查看这篇文章:steemit.com/programming/@elsanto/
  • @DanieleD是什么让你说这是sql的痛苦?我正在考虑将它用于某些SQL查询,因为我希望能够将源代码复制并粘贴到其他上下文中。是不是它不解析转义序列?
  • @KyleHeuton:大概Daniele D在他/她的SQL查询中使用反引号字符(正如MySQL用户经常做的那样),并且发现将它表示为"+"`"+"并打破复制和可匹配性是很痛苦的。
  • 如果人们对MySQL有这个问题,请注意您始终可以设置会话sql模式,例如SET SESSION sql_mode = 'ANSI_QUOTES';Treat" as an identifier quote character (like the backtick quote character) and not as a string quote character.然后确保使用撇号'作为我见过的每个SQL数据库的字符串文字。请参阅dev.mysql.com/doc/refman/5.7/en/

你可以写:

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

这与:

1
"line 1line 2line3"

与使用后退标记不同,它将保留转义字符。请注意,"+"必须位于"前导"行,即:

1
2
"line 1"
+"line 2"

生成错误。

  • 此解决方案与Python的多行字符串不同。它将字符串文字拆分为多行,但字符串本身不包含多行。
  • 由于这会保留转义字符,因此可以使用
    简单地添加新行,并且更容易使用动态字符串等。如果我是正确的,接受的答案实际上是代码中的静态字符串,使其看起来很漂亮。
  • 这也不是非常低效吗?让字符串为3x 6 char序列:6 + 2 * 6 + 3 * 6 = 36个字符,当最佳值为18时分配(因为字符串是不可变的,每次添加两个字符串时,创建一个新字符串,长度为两个字符串连接)。
  • @ N0thing:如果只有字符串文字,则编译器将优化时没有运行时差异。但编译时间差异很小(微秒甚至纳秒)。
  • 我相信这是获得带有CRLF行结尾的多行字符串文字的最佳方法
  • 这在fmt.Sprintf("very long dynamic string, such as SQL")上有很大作用

来自字符串文字:

  • 原始字符串文字支持多行(但不解释转义字符)
  • 解释的字符串文字解释转义字符,如'
    '。

但是,如果你的多行字符串必须包含一个反引号(`),那么你将不得不使用一个解释的字符串文字:

1
2
3
4
`line one
  line two ` +
"`" + `line three
line four`

你不能直接在原始字符串文字(``xx \)中放置反引号(`)。
你必须使用(如"如何在反引号字符串中放置反引号?"中所述):

1
 +"`" + ...

将原始字符串文字用于多行字符串:

1
2
3
4
5
6
func main(){
    multiline := `line
by line
and line
after line`
}

原始字符串文字

Raw string literals are character sequences between back quotes, as in `foo`. Within the quotes, any character may appear except back quote.

一个重要的部分是原始文字不仅仅是多行而且多行并不是它的唯一目的。

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

因此,不会解释转义,并且刻度之间的新行将是真正的新行。

1
2
3
4
5
6
7
8
9
10
11
12
13
func main(){
    multiline := `line
by line

and line

after line`

    //
 will be just printed.
    // But new lines are there too.
    fmt.Print(multiline)
}

级联

可能你想要打破你的长行,你不需要新行。在这种情况下,您可以使用字符串连接。

1
2
3
4
5
6
7
8
func main(){
    multiline :="line" +
           "by line" +
           "and line" +
           "after line"

    fmt.Print(multiline) // No new lines here
}

由于""被解释,因此将解释字符串文字转义。

1
2
3
4
5
6
7
8
9
10
11
func main(){
    multiline :="line" +
           "by line
" +
           "and line
" +
           "after line"

    fmt.Print(multiline) // New lines as interpreted

}

去和多线字符串

使用后退标记,您可以使用多行字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)
}

而不是使用双引号(")或单引号符号('),而是使用反向标记来定义字符串的开头和结尾。然后,您可以跨行包装它。

If you indent the string though, remember that the white space will
count.

请检查操场并进行实验。


你可以把内容放在它周围,比如说

1
2
3
var hi = `I am here,
hello,
`

你必须非常小心go格式和行间距,一切都很重要,这里是一个工作样本,试试吧https://play.golang.org/p/c0zeXKYlmF

1
2
3
4
5
6
7
8
9
package main

import"fmt"

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

你可以使用原始文字。

1
2
s:=`stack
overflow`

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

1
2
3
4
fmt.Sprintf("Hello World
How are you doing today
Hope all is well with your go
And code")

否则你可以使用raw string

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