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)

fmt.Println
fmt.Println
"Hello, World!"Hello, World!
"Hello, World!"Hello, World!

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)

`"
`"
`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.

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

rawinterpreted
rawinterpreted

转义字符 (Escape Characters)

\
\

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

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

Escape CharacterHow it formats
\Backslash
"Double Quote
\nLine Break
\tTab (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!"
\"
\"
\n
\n
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
\t
\t
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.

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

raw
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.`
+
+
"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语言中的文本格式的几种方法。 通过使用转义字符或原始字符串之类的技术,我们可以确保将程序的字符串正确显示在屏幕上,以便最终用户能够轻松读取所有输出文本。

golang 格式化字符串