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字符串的方法,以确保所有输出文本的格式正确。
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.
但是某些字符串值可能需要包含引号,例如在引用源代码时。 由于字符串文字和字符串值不相等,因此通常有必要向字符串文字添加其他格式,以确保按照我们期望的方式显示字符串值。
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中使用字符串简介》教程。
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"
。
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.
虽然反引号可以使打印和阅读冗长的文本变得更加容易,但是如果您需要解释的字符串文字,则需要使用双引号。
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.
通过使用给定字符串周围的反引号构造原始字符串,我们可以保留反斜杠和其他用作转义符的字符。
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 格式化字符串