string 你如何在 Go 中编写多行字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7933460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you write multiline strings in Go?
提问by aeter
Does Go have anything similar to Python's multiline strings:
Go 有没有类似于 Python 的多行字符串:
"""line 1
line 2
line 3"""
If not, what is the preferred way of writing strings spanning multiple lines?
如果不是,编写跨越多行的字符串的首选方式是什么?
回答by Mark Byers
According to the language specificationyou can use a raw string literal, where the string is delimited by backticks instead of double quotes.
根据语言规范,您可以使用原始字符串文字,其中字符串由反引号而不是双引号分隔。
`line 1
line 2
line 3`
回答by mddkpp at gmail.com
You can write:
你可以写:
"line 1" +
"line 2" +
"line 3"
which is the same as:
这与:
"line 1line 2line3"
Unlike using back ticks, it will preserve escape characters. Note that the "+" must be on the 'leading' line i.e.:
与使用反引号不同,它将保留转义字符。请注意,“+”必须在“前导”行上,即:
"line 1"
+"line 2"
generates an error.
产生错误。
回答by VonC
From String literals:
从字符串文字:
- raw string literal supports multiline (but escaped characters aren't interpreted)
- interpreted string literal interpret escaped characters, like '
\n
'.
- 原始字符串文字支持多行(但不解释转义字符)
- 解释字符串字面解释转义字符,如'
\n
'。
But, if your multi-line string has to include a backquote (`), then you will have to use an interpreted string literal:
但是,如果您的多行字符串必须包含反引号 (`),那么您将不得不使用解释型字符串文字:
`line one
line two ` +
"`" + `line three
line four`
You cannot directly put a backquote (`) in a raw string literal (``xx\
).
You have to use (as explained in "how to put a backquote in a backquoted string?"):
您不能直接在原始字符串文字 (``xx \
) 中放置反引号 (` )。
您必须使用(如“如何在反引号字符串中放置反引号?”中所述):
+ "`" + ...
回答by I159
Use raw string literals for multi-line strings:
对多行字符串使用原始字符串文字:
func main(){
multiline := `line
by line
and line
after line`
}
Raw string literals
原始字符串文字
Raw string literals are character sequences between back quotes, as in
`foo`
. Within the quotes, any character may appear except back quote.
原始字符串文字是反引号之间的字符序列,如
`foo`
. 在引号内,除了反引号外,任何字符都可以出现。
A significant part is that is rawliteral not just multi-line and to be multi-line is not the only purpose of it.
一个重要的部分是原始文字不仅仅是多行,多行并不是它的唯一目的。
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...
原始字符串文字的值是由引号之间的未解释(隐式 UTF-8 编码)字符组成的字符串;特别是,反斜杠没有特殊含义......
So escapes will not be interpreted and new lines between ticks will be real new lines.
所以转义不会被解释,刻度之间的新行将是真正的新行。
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)
}
Concatenation
级联
Possibly you have long line which you want to break and you don't need new lines in it. In this case you could use string concatenation.
可能你有很长的线要打破,你不需要在其中加入新的线。在这种情况下,您可以使用字符串连接。
func main(){
multiline := "line " +
"by line " +
"and line " +
"after line"
fmt.Print(multiline) // No new lines here
}
Since " " is interpreted string literal escapes will be interpreted.
由于“”是解释字符串文字转义将被解释。
func main(){
multiline := "line " +
"by line \n" +
"and line \n" +
"after line"
fmt.Print(multiline) // New lines as interpreted \n
}
回答by ASHWIN RAJEEV
Go and multiline strings
Go 和多行字符串
Using back ticks you can have multiline strings:
使用反勾号,您可以拥有多行字符串:
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)
}
Instead of using either the double quote (“) or single quote symbols (‘), instead use back-ticks to define the start and end of the string. You can then wrap it across lines.
不要使用双引号 (") 或单引号 ('),而是使用反引号来定义字符串的开头和结尾。然后,您可以将其跨行包装。
If you indent the string though, remember that the white space will count.
但是,如果您缩进字符串,请记住空格会计数。
Please check the playground and do experiments with it.
请检查操场并对其进行实验。
回答by liam
You can put content with `` around it, like
你可以用`` 把内容放在它周围,比如
var hi = `I am here,
hello,
`
回答by David
You have to be very careful on formatting and line spacing in go, everything counts and here is a working sample, try it https://play.golang.org/p/c0zeXKYlmF
你必须非常小心 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)
}
回答by Prabesh P
you can use raw literals. Example
您可以使用原始文字。例子
s:=`stack
overflow`
回答by Ofonime Francis
For me this is what I use if adding \n
is not a problem.
对我来说,如果添加\n
不是问题,这就是我使用的。
fmt.Sprintf("Hello World\nHow are you doing today\nHope all is well with your go\nAnd code")
Else you can use the raw string
否则你可以使用 raw string
multiline := `Hello Brothers and sisters of the Code
The grail needs us.
`