本文翻译自:How do you write multiline strings in Go?

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? 如果不是,编写跨多行字符串的首选方式是什么?


#1楼

参考:https://stackoom.com/question/XHr2/如何在Go中编写多行字符串


#2楼

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 oneline two ` +
"`" + `line three
line four`

You cannot directly put a backquote (`) in a raw string literal (``xx \\ ). 您不能在原始字符串文字(``xx \\ )中直接添加反引号(`)。
You have to use (as explained in " how to put a backquote in a backquoted string? "): 您必须使用(如“ 如何在反引号中的字符串中加上反引号? ”中所述):

 + "`" + ...

#3楼

You can put content with `` around it, like 您可以在内容周围加上``

var hi = `I am here,
hello,
`

#4楼

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` . 原始字符串文字是反引号之间的字符序列,如`foo` Within the quotes, any character may appear except back quote. 在引号内,除反引号外,任何字符都可能出现。

A significant part is that is raw literal 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; 原始字符串文字的值是由引号之间未解释(隐式为UTF-8编码)的字符组成的字符串。 in particular, backslashes have no special meaning... 特别是反斜杠没有特殊含义...

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
}

#5楼

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 您必须非常注意格式和行间距,一切都很重要,这是一个有效的示例,请尝试一下https://play.golang.org/p/c0zeXKYlmF

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

#6楼

you can use raw literals. 您可以使用原始文字。 Example

s:=`stack
overflow`

如何在Go中编写多行字符串?相关推荐

  1. linux命令行运行c程序,如何在Linux中编写和运行C程序

    Linux正在成为开发人员的编程天堂,成为开源和免费操作系统. Turbo C编译器已经是一种编译程序的旧方法,所以让程序员转向Linux以获得新的编程环境. 在本文中,我们将解释如何编写,编译和运行 ...

  2. matlab矩阵指定行最大值,求Matlab程序:在2行矩阵中,如何求第1行最大值和第2行相应的最大,请问,如何在MATLAB中编写程序实现求两矩阵A*B,A.*...

    导航:网站首页 > 求Matlab程序:在2行矩阵中,如何求第1行最大值和第2行相应的最大,请问,如何在MATLAB中编写程序实现求两矩阵A*B,A.* 求Matlab程序:在2行矩阵中,如何求 ...

  3. python 正则表达式 前瞻_我应该如何在python中编写这个正则表达式(How should I write this regex in python)...

    我应该如何在python中编写这个正则表达式(How should I write this regex in python) 我有字符串. st = "12345 hai how r u ...

  4. 在JavaScript中创建多行字符串

    我在Ruby中有以下代码. 我想将此代码转换为JavaScript. JS中的等效代码是什么? text = <<"HERE" This Is A Multiline ...

  5. 如何在Go中编写防弹代码:不会失败的服务器工作流程

    by Tal Kol 通过塔尔科尔 如何在Go中编写防弹代码:不会失败的服务器工作流程 (How to write bulletproof code in Go: a workflow for ser ...

  6. nodejs命令行执行程序_在NodeJS中编写命令行应用程序

    nodejs命令行执行程序 by Peter Benjamin 彼得·本杰明(Peter Benjamin) 在NodeJS中编写命令行应用程序 (Writing Command-Line Appli ...

  7. JSON中的多行字符串

    我正在用JSON格式编写一些数据文件,并希望将一些非常长的字符串值分成多行. 使用python的JSON模块我得到了很多错误,无论我使用\\或\\n作为转义. 是否可以在JSON中使用多行字符串? 这 ...

  8. 如何在Ruby中编写switch语句

    如何在Ruby中编写switch语句? #1楼 案例...当 在Chuck的答案中添加更多示例: 带参数: case a when 1puts "Single value" whe ...

  9. 手机nfc_如何在Android中编写NFC标签

    手机nfc 这篇文章介绍了如何在Android中使用NFC编写智能标签. Android智能手机不仅能够读取包含URL,电话号码等数据的NFC标签,但使用Android NFC Api可以写入NFC标 ...

最新文章

  1. 第39次《中国互联网络发展状况统计报告》发布
  2. python语言if语句-Python中的if判断语句入门
  3. Leetcode 剑指 Offer 11. 旋转数组的最小数字 (每日一题 20210916)
  4. mysql课程设计案例_JAVA中MySQL建立连接
  5. EasyNVR摄像机网页无插件直播方案H5前端构建之:关于接口调用常见的一些问题(401 Unauthorized)...
  6. 2020CCPC(长春) - Combination Lock(二分图博弈)
  7. 一个有趣的问题,讨论讨论
  8. 这家AI公司用面具破解中国人脸识别系统!微信、支付宝、火车站无一幸免
  9. JavaScript多种跨域方式
  10. Python基础-列表(列表常用函数/列表遍历)
  11. python 表格格式输出_简单介绍python输出列表元素的所有排列形式
  12. 日常邮件用语(一)网摘学习
  13. leetcode—19.二叉树遍历相关题目leetcode总结
  14. 蓝桥杯2017年第八届C/C++省赛B组第二题-等差素数列
  15. windows jdk8
  16. 【Latex学习】在IEEEtran模板中使用algorithm环境
  17. win10 如何修改 C:\Users\用户名文件夹
  18. 卡内基梅隆的计算机科学专业,卡内基梅隆大学计算机科学专业
  19. 对物联网的感悟_对物联网产业的理解 对物联网的感悟
  20. 如何提高深度学习的泛化能力?

热门文章

  1. Android必知必会-Android Studio下配置和使用Lambda
  2. Handler消息机制(一):Linux的epoll机制
  3. Chrome 输入文字卡死
  4. java 编码二进制写法、十六进制用源代码表示
  5. Android 修改手机hosts域名 (绑定host域名 )
  6. RandomAecessFile open failed: EISDIR (Is a directory)
  7. 【剑指offer-Java版】09斐波那契数列
  8. 大家都说 Java 反射效率低,为什么呢?
  9. SparkStreaming读取Socket数据
  10. 减少资源消耗方法之一:减少状态图片