golang 格式化字符串

As strings are often made up of written text, there are many instances when we may want to have greater control over how strings look to make them more readable for humans through punctuation, line breaks, and indentation.

由于字符串通常是由书面文本组成的,因此在许多情况下,我们可能希望更好地控制字符串的外观,以便通过标点,换行和缩进使字符串对人类更具可读性。

In this tutorial, we’ll go over some of the ways we can work with Go strings to make sure that all output text is formatted correctly.

在本教程中,我们将介绍一些使用Go字符串的方法,以确保所有输出文本的格式正确。

字符串文字 (String Literals)

Let’s first differentiate between a string literal and a string value. A string literal is what we see in the source code of a computer program, including the quotation marks. A string value is what we see when we call the fmt.Println function and run the program.

首先让我们区分字符串文字字符串值 。 字符串文字是我们在计算机程序的源代码中看到的,包括引号。 字符串值是调用fmt.Println函数并运行程序时看到的。

In the “Hello, World!” program, the string literal is "Hello, World!" while the string value is Hello, World! without the quotation marks. The string value is what we see as the output in a terminal window when we run a Go program.

在“你好,世界!”中 程序中,字符串文字为"Hello, World!" 而字符串值是Hello, World! 不带引号。 字符串值是运行Go程序时在终端窗口中看到的输出。

But some string values may need to include quotation marks, like when we are quoting a source. Because string literals and string values are not equivalent, it is often necessary to add additional formatting to string literals to ensure that string values are displayed the way in which we intend.

但是某些字符串值可能需要包含引号,例如在引用源代码时。 由于字符串文字和字符串值不相等,因此通常有必要向字符串文字添加其他格式,以确保按照我们期望的方式显示字符串值。

行情 (Quotes)

Because we can use back quotes (`) or double quotes (") within Go, it is simple to embed quotes within a string by using double quotes within a string enclosed by back quotes:

由于我们可以在Go中使用反引号( ` )或双引号( " ),因此可以通过在反引号引起的字符串中使用双引号来将引号嵌入字符串中,这很简单:

`Sammy says, "Hello!"`

Or, to use a back quote, you can enclose the string in double quotes:

或者,要使用反引号,可以将字符串用双引号引起来:

"Sammy likes the `fmt` package for formatting strings.."

In the way we combine back quotes and double quotes, we can control the display of quotation marks and back quotes within our strings.

通过组合反引号和双引号,我们可以控制字符串中引号和反引号的显示。

It’s important to remember that using back quotes in Go creates a raw string literal, and using double quotes creates an interpreted string literal. To learn more about the difference, read the An Introduction to Working with Strings in Go tutorial.

重要的是要记住,在Go中使用反引号会创建raw字符串文字,而在双引号中则会创建interpreted字符串文字。 要了解有关差异的更多信息,请阅读《 在Go中使用字符串简介》教程。

转义字符 (Escape Characters)

Another way to format strings is to use an escape character. Escape characters are used to tell the code that the following character has a special meaning. Escape characters all start with the backslash key (\) combined with another character within a string to format the given string a certain way.

格式化字符串的另一种方法是使用转义字符 。 转义字符用于告诉代码以下字符具有特殊含义。 转义字符均以反斜杠键( \ )开头,并与字符串中的另一个字符结合使用,以某种方式格式化给定的字符串。

Here is a list of several of the common escape characters:

以下是一些常见的转义字符的列表:

Escape Character How it formats
\ Backslash
" Double Quote
\n Line Break
\t Tab (horizontal indentation)
转义符 格式如何
\ 反斜杠
双引号
\ n 越线
\ t 制表符(水平缩进)

Let’s use an escape character to add the quotation marks to the example on quotation marks above, but this time we’ll use double quotes to denote the string:

让我们使用转义字符将引号添加到上面引号的示例中,但是这次我们将使用双引号来表示字符串:

fmt.Println("Sammy says, \"Hello!\"")

Output
Sammy says, "Hello!"

By using the escape character \" we are able to use double quotes to enclose a string that includes text quoted between double quotes.

通过使用转义符\"我们可以使用双引号将包含双引号之间包含的文本的字符串括起来。

We can use the \n escape character to break lines without hitting the enter or return key:

我们可以使用\n转义字符来换行,而无需按Enter或return键:

fmt.Println("This string\nspans multiple\nlines.")

Output
This string
spans multiple
lines.

We can combine escape characters, too. Let’s print a multi-line string and include tab spacing for an itemized list, for example:

我们也可以组合转义字符。 让我们打印一个多行字符串,并为逐项列出列表的制表符间距,例如:

fmt.Println("1.\tShark\n2.\tShrimp\n10.\tSquid")

Output
1.      Shark
2.      Shrimp
10.     Squid

The horizontal indentation provided with the \t escape character ensures alignment within the second column in the preceding example, making the output extremely readable for humans.

带有\t转义字符的水平压痕可确保在前面的示例中第二列内对齐,从而使输出对人类而言非常可读。

Escape characters are used to add additional formatting to strings that may be difficult or impossible to achieve. Without escape characters, you would not be able to construct the string Sammy says, "I like to use the `fmt` package".

转义字符用于向字符串添加其他格式,这些格式可能难以实现或无法实现。 没有转义字符,您将无法构造字符串Sammy says, "I like to use the `fmt` package"

多行 (Multiple Lines)

Printing strings on multiple lines can make text more readable to humans. With multiple lines, strings can be grouped into clean and orderly text, formatted as a letter, or used to maintain the linebreaks of a poem or song lyrics.

在多行上打印字符串可以使文本对人类更易读。 通过多行,可以将字符串分组为整洁有序的文本,格式为字母,或用于维护诗歌或歌曲歌词的换行符。

To create strings that span multiple lines, back quotes are used to enclose the string. Keep in mind that while this will preserve the line returns, it is also creating a raw string literal.

要创建跨越多行的字符串,请使用反引号将字符串括起来。 请记住,尽管这将保留行返回值,但同时还会创建raw字符串文字。

`
This string is on
multiple lines
within three single
quotes on either side.
`

You will notice if you print this that there is a leading and trailing return:

您会注意到,如果打印此内容,则会有前导和尾随收益:


Output
This string is on
multiple lines
within three single
quotes on either side.

To avoid this, you need to put the first line immediately following the back quote and end the last with the back quote.

为避免这种情况,您需要将第一行放在反引号之后,并在最后一行加上反引号。

`This string is on
multiple lines
within three single
quotes on either side.`

If you need to create an interpreted string literal, this can be done with double quotes and the + operator, but you will need to insert your own line breaks.

如果需要创建解释的字符串文字,可以使用双引号和+运算符来完成,但是您需要插入自己的换行符。

"This string is on\n" +
"multiple lines\n" +
"within three single\n" +
"quotes on either side."

While back quotes can make it easier to print and read lengthy text, if you need an interpreted string literal, you will need to use double quotes.

虽然反引号可以使打印和阅读冗长的文本变得更加容易,但是如果您需要解释的字符串文字,则需要使用双引号。

原始字符串文字 (Raw String Literals)

What if we don’t want special formatting within our strings? For example, we may need to compare or evaluate strings of computer code that use the backslash on purpose, so we won’t want Go to use it as an escape character.

如果我们不想在字符串中使用特殊格式怎么办? 例如,我们可能需要比较或评估故意使用反斜杠的计算机代码字符串,因此我们不希望Go将其用作转义字符。

A raw string literal tells Go to ignore all formatting within a string, including escape characters.

原始字符串文字告诉Go忽略字符串中的所有格式,包括转义字符。

We create a raw string by using back quotes around the string:

我们通过在字符串周围使用反引号来创建原始字符串:

fmt.Println(`Sammy says,\"The balloon\'s color is red.\"`)

Output
Sammy says,\"The balloon\'s color is red.\"

By constructing a raw string by using back quotes around a given string, we can retain backslashes and other characters that are used as escape characters.

通过使用给定字符串周围的反引号构造原始字符串,我们可以保留反斜杠和其他用作转义符的字符。

结论 (Conclusion)

This tutorial went over several ways to format text in Go through working with strings. By using techniques such as escape characters or raw strings, we are able to ensure that the strings of our program are rendered correctly on-screen so that the end user is able to easily read all of the output text.

本教程介绍了通过使用字符串来设置Go语言中的文本格式的几种方法。 通过使用转义字符或原始字符串之类的技术,我们可以确保将程序的字符串正确显示在屏幕上,以便最终用户能够轻松读取所有输出文本。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-format-strings-in-go

golang 格式化字符串

golang 格式化字符串_如何在Go中格式化字符串相关推荐

  1. php根据字符串分割字符串_如何在PHP中按字符串分割字符串?

    php根据字符串分割字符串 How to split a string by string in PHP? For example, 如何在PHP中按字符串分割 字符串 ? 例如, "a s ...

  2. mysql中转换成字符串_如何在R中转换字符串的大小写?

    mysql中转换成字符串 Hello, folks. In this tutorial we are going to convert the case of the string in R. The ...

  3. java 格式化 浮点数_如何在javascript中格式化浮点数?

    回答(13) 2 years ago 我猜的关键是首先正确地向上舍入,然后你可以将它转换为String . function roundOf(n, p) { const n1 = n * Math.p ...

  4. ruby字符串截取字符串_如何在Ruby中附加字符串?

    ruby字符串截取字符串 There are multiple ways to do the required but we will study about three of them. 有多种方法 ...

  5. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  6. python中用什么函数读取字符串_如何在Python中获得函数名作为字符串?

    在Python中,如何在不调用函数的情况下以字符串的形式获得函数名? 1 2 3 4def my_function(): pass print get_function_name_as_string( ...

  7. regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾

    regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...

  8. java中转json字符串_如何在Java中转义JSON字符串-Eclipse IDE技巧

    java中转json字符串 在Java应用程序中工作或进行JSON解析时,通常很常见的做法是从某些资源(例如RESTful Web服务)中复制粘贴JSON字符串,然后使用Jackson库解析JSON. ...

  9. java字符串字符排列组合_如何在Java中查找字符串的所有排列

    java字符串字符排列组合 In this tutorial, we will learn how to find the permutation of a String in a Java Prog ...

最新文章

  1. Android Dialog 的使用总结(AlertDialog)
  2. Owncloud-X安装配置
  3. Scrum指南新版发布,再添新概念—Product Goal
  4. DPDK — CLI 指令行模块
  5. maven系列一:pom.xml文件详解
  6. go 查看全局安装了哪些包_如何用 GVM 管理 Go 项目
  7. 无论发生了什么生活_无论如何,一个开放团队的经理会做什么?
  8. oxp开放型可变长协议_培养开放型领导者
  9. 帆软报表设计器菜单栏介绍之一
  10. 深入浅出CChart 每日一课——快乐高四第九课 于无声处,CChart内置功能介绍之数据存取篇...
  11. 【Mysql】之基础sql语句模板
  12. ajax返回的java list_ssm+ajax异步请求返回list遍历
  13. 载入java VM 时Windows 出现错误:2
  14. Simulink电力系统仿真-三相短路
  15. 删除卸载企业微信后的残留文件
  16. GT9xxxxx系列------如何加入电源管理模块
  17. 如何保障企业业务流程的落地实施?
  18. .NET Framework各个版本(1.0 - 2.0)
  19. 什么是RPC?RPC好处?常用的RPC框架?
  20. 从0到1搭建大数据平台之调度系统

热门文章

  1. 老妈终于会用电脑了!
  2. emoji表情对应表
  3. OSChina 周六乱弹 ——在你玩的时候,妹子都在干这个
  4. STM32定时器详解——TIM详解
  5. 适配三星Galaxy S8及S8+
  6. Java项目茶叶溯源系统(java+SSM+JSP+bootstrap+layUI+mysql)
  7. 巧用清单把待办事件安排的井井有条
  8. 【论文】多区域摄像头的人脸实时对比设计
  9. 再谈:互联网基本思维就是免费,本质就是资本运作
  10. metal升android5.1,魅蓝 metal 详尽图文评测 - 全金属机身+ Flyme 5.1 系统+指纹识别