Go语言中是否有类似Python多行字符串的东西:
"""line 1
line 2
line 3"""
如果没有,那么编写跨多行的字符串的首选方法是什么?
$(abcd)
)也很麻烦。 - Ivan VučicaSET SESSION sql_mode ='ANSI_QUOTES';
这将“将双引号视为标识符引用字符(如反引号引用字符),而不是字符串引用字符。” 然后只需确保对于每个SQL数据库都使用撇号 '
作为字符串文字。请参见 https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes - Davos你可以这样写:
"line 1" +
"line 2" +
"line 3"
这与以下代码相同:
"line 1line 2line 3"
使用双引号不同于使用反引号,它将保留转义字符。请注意,“+” 必须在“前导”的行上 - 例如,以下内容将会生成错误:
"line 1"
+"line 2"
\n
轻松添加新行,这样就更容易处理动态字符串等。如果我没错的话,被接受的答案确实是用于静态代码字符串的美观呈现。 - RisingSunfmt.Sprintf("非常长的动态字符串,例如 SQL")
上运行得非常好。 - coanor使用原始字符串字面量来表示多行字符串:
func main(){
multiline := `line
by line
and line
after line`
}
原始字符串字面量是用反引号括起来的字符序列,例如
`foo`
。在引号内,任何字符都可以出现,除了反引号。
重要的是原始字面量而不仅仅是多行文本,其目的也不仅仅是支持多行文本。
原始字符串字面量的值是由引号之间未解释的(隐含UTF-8编码)字符组成的字符串;特别地,反斜杠没有特殊意义......
所以转义字符将不会被解释和反引号之间的换行符将成为真正的换行符。
func main(){
multiline := `line
by line \n
and line \n
after line`
// \n will be just printed.
// But new lines are there too.
fmt.Print(multiline)
}
可能你有一行很长的文本需要断开成多行显示,但是你不想在其中插入新的换行符。这时候你可以使用字符串连接来实现。
func main(){
multiline := "line " +
"by line " +
"and line " +
"after line"
fmt.Print(multiline) // No new lines here
}
由于“”是解释型字符串文字,因此转义字符将被解释。
func main(){
multiline := "line " +
"by line \n" +
"and line \n" +
"after line"
fmt.Print(multiline) // New lines as interpreted \n
}
从字符串字面值:
\n
'。但是,如果您的多行字符串必须包含反引号(`),则必须使用解释的字符串字面值:
`line one
line two ` +
"`" + `line three
line four`
您不能在原始字符串字面值中直接放置反引号 (`) (``xx\
)。
您必须使用以下方法(正如在“如何在反引号字符串中放置反引号?”中所解释的那样):
+ "`" + ...
使用反引号可以创建多行字符串:
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)
}
使用反引号(`)来定义字符串的开始和结束,而不是双引号(“)或单引号符号(‘)。然后可以跨行包装它。
如果缩进字符串,则请记住空格将会被计算在内。
请检查playground并进行实验。
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)
}
var hi = `I am here,
hello,
`
在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`
对于我来说,我需要使用`反引号/重音符号,并且只需编写一个简单的测试。
+ "`" + ...
这个东西不美观且不方便
所以我用一个字符例如:U+1F42C来代替它
一个演示
myLongData := `line1
line2 aaa
line3
` // maybe you can use IDE to help you replace all ` to
myLongData = strings.ReplaceAll(myLongData, "", "`")
+ "`"
与 replaceAll(, "", "`")
的比较
package main
import (
"strings"
"testing"
)
func multilineNeedGraveWithReplaceAll() string {
return strings.ReplaceAll(`line1
line2
line3 aaa`, "", "`")
}
func multilineNeedGraveWithPlus() string {
return `line1
line2
line3` + "`" + "aaa" + "`"
}
func BenchmarkMultilineWithReplaceAll(b *testing.B) {
for i := 0; i < b.N; i++ {
multilineNeedGraveWithReplaceAll()
}
}
func BenchmarkMultilineWithPlus(b *testing.B) {
for i := 0; i < b.N; i++ {
multilineNeedGraveWithPlus()
}
}
命令提示符
go test -v -bench=. -run=none -benchmem
查看更多 testing.B
输出
goos: windows
goarch: amd64
pkg: tutorial/test
cpu: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
BenchmarkMultilineWithReplaceAll
BenchmarkMultilineWithReplaceAll-8 12572316 89.95 ns/op 24 B/op 1 allocs/op
BenchmarkMultilineWithPlus
BenchmarkMultilineWithPlus-8 1000000000 0.2771 ns/op 0 B/op 0 allocs/op
PASS
ok tutorial/test 7.566s
是的,+ "`"
的性能比其他方式更好。