如何在 Go 中编写多行字符串?
Go 是否有类似于 Python 的多行字符串的功能:
"""line 1
line 2
line 3"""
如果没有,那么编写跨多行字符串的首选方式是什么?
Does Go have anything similar to Python's multiline strings:
"""line 1
line 2
line 3"""
If not, what is the preferred way of writing strings spanning multiple lines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
根据语言规范,您可以使用原始字符串文字,其中字符串被分隔用反引号代替双引号。
According to the language specification, you can use a raw string literal, where the string is delimited by backticks instead of double quotes.
您可以编写:
与:
与使用反引号不同,它将保留转义字符。请注意,“+”必须位于“前导”行 - 例如,以下内容将生成错误:
You can write:
which is the same as:
Unlike using back ticks, it will preserve escape characters. Note that the "+" must be on the 'leading' line - for instance, the following will generate an error:
对多行字符串使用原始字符串文字:
原始字符串文字
一个重要的部分是,原始文字不仅仅是多行,而且多行并不是它的唯一目的。
因此转义符不会被解释,并且刻度之间的新行将是真正的新行。
连接
可能你有很长的一行,你想打破它,并且你不需要在其中添加新行。在这种情况下,您可以使用字符串连接。
由于“”被解释为字符串文字转义将被解释。
Use raw string literals for multi-line strings:
Raw string literals
A significant part is that is raw literal not just multi-line and to be multi-line is not the only purpose of it.
So escapes will not be interpreted and new lines between ticks will be real new lines.
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.
Since " " is interpreted string literal escapes will be interpreted.
来自 字符串文字:
\n
”。但是,如果您的多行字符串必须包含反引号 (`),那么您将必须使用解释的字符串文字:
您不能直接将反引号 (`) 放入原始字符串文字 (``xx
\
)。您必须使用(如“如何在反引号字符串中放置反引号?”中所述):
From String literals:
\n
'.But, if your multi-line string has to include a backquote (`), then you will have to use an interpreted string literal:
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?"):
Go 和多行字符串
使用反引号,您可以拥有多行字符串:
不要使用双引号 (“) 或单引号符号 ('),而是使用反引号来定义字符串的开头和结尾。然后你可以将它绕行。
请检查 playground 并用它做实验。
Go and multiline strings
Using back ticks you can have multiline strings:
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.
Please check the playground and do experiments with it.
在 Go 中创建多行字符串实际上非常简单。声明或分配字符串值时只需使用反引号 (`) 字符即可。
Creating a multiline string in Go is actually incredibly easy. Simply use the backtick (`) character when declaring or assigning your string value.
对于那些可读性比性能更重要的用例,我建议使用这种简单的方法:
这就是你使用它的方式:
它生成:
基本上
为什么我更喜欢它而不是反引号?
因为您可以使内容保持一致且易于阅读:
For those use cases where readability is more important than performance then i'd suggest this simple method:
This is how you use it:
It generates:
basically
Why i like it more than the backticks?
Because you can keep things aligned and easy to read:
您可以在内容周围加上``,例如
You can put content with `` around it, like
你必须非常小心 go 中的格式和行距,一切都很重要,这里是一个工作示例,尝试一下 https://play.golang.org/p/c0zeXKYlmF
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
您可以使用原始文字。
例子
you can use raw literals.
Example
对我来说,我需要使用 ` 重音/反引号 和只写一个简单的测试
是丑陋且不方便的
,所以我以一个字符为例:
For me, I need to use ` grave accent/backquote and just write a simple test
is ugly and inconvenient
so I take a characterfor example: ???? U+1F42C to replace it
a demo
Performance and Memory Evaluation
+ "`"
v.s.replaceAll(, "????", "`")
cmd
output
Yes, The
+ "`"
has a better performance than the other.对我来说,如果添加
\n
不是问题,我就使用这个。否则您可以使用
原始字符串
For me this is what I use if adding
\n
is not a problem.Else you can use the
raw string
我将
+
与第一个空字符串一起使用。这允许某种可读的格式,并且适用于 const。I use the
+
with an empty first string. This allows a somehow readable format and it works forconst
.