Preface
Since strings are usually composed of written text, in many cases we may want to better control the appearance of the string through punctuation, line breaks, and indentation to make it easier to read.
In this tutorial, we will cover some ways to handle go strings to ensure that all output text is formatted correctly.
String literal
Let's first distinguishString literalandString value. String literals are what we see in the source code of computer programs, including quotes. When we callWhen the function and the program is running, we see a string.
In the "Hello, World!" program, the string literal is"Hello, World!"
, and the string value is Hello, World!
, no quotes. This string value is the output we see in the terminal window when running the Go program.
But some string values may need to contain quotes, such as when we quote a source. Because string literals and string values are not equal, it is usually necessary to add additional formatting to string literals to ensure that string values are displayed the way we want them to.
quotation marks
Because we can use backticks in Go (`
) or double quotes ("
), by using double quotes in strings enclosed by backquotes, it is easy to embed quotes into strings:
`Sammy says, "Hello!"`
Alternatively, to use post-quotes, enclose the string in double quotes:
"Sammy likes the `fmt` package for formatting strings.."
By using backticks and double quotes, we can control the display of quotes and backticks in a string.
It is important to remember that using backticks in Go creates araw
String literals, while using double quotes creates ainterpret
String literal.
Escape characters
Another way to format strings is to useEscape characters. Escape characters are used to tell the code that the characters below have special meanings. All escape characters are backslashed (\
) and another character in the string start, formatting the given string somehow.
Here are some common escape characters:
Escape characters | How does it format |
---|---|
|Backslash | |
" | Double quotes |
\n | Line breaks |
\t | Tab (horizontal indentation) |
Let's add quotes to the above quote example using escape characters, but this time we'll use double quotes to represent the string:
("Sammy says, \"Hello!\"")
OutputSammy says, "Hello!"
By using escape characters\"
, we can use double quotes to enclose strings containing text in double quotes.
We can use\n
Escape characters to wrap lines without typing Enter or Enter:
("This string\nspans multiple\nlines.")
OutputThis string
spans multiple
lines.
We can also combine escape characters. Here is a multi-line string with tab spacing for the sub-item list, for example:
("1.\tShark\n2.\tShrimp\n10.\tSquid")
Output1. Shark
2. Shrimp
10. Squid
In the previous example,\t
The horizontal indent provided by the escape characters ensures alignment within the second column, making the output very readable.
Escape characters are used to add additional formats to strings that can be difficult or impossible to implement. You won't be able to construct strings without escape charactersSammy says, "I like to use the `fmt` package"
。
Multiple lines
Printing strings into multiple lines can improve the readability of text. For multiple lines, strings can be combined into clean and ordered text, formatted as letters, or line breaks used to keep poems or song lyrics.
To create a string that spans multiple lines, you need to enclose the string with backticks. Remember that while this will keep the row return, it also creates araw
String literal.
` This string is on multiple lines within three single quotes on either side. `
Output This string is on multiple lines within three single quotes on either side.
To avoid this, you need to place the first line after the quotes and end the last line after the quotes.
`This string is on multiple lines within three single quotes on either side.`
If you need to create an interpreted string literal, you can use double quotes and+
operator, but you need to insert your own newline.
"This string is on\n" + "multiple lines\n" + "within three single\n" + "quotes on either side."
While backquotes can make long text easier to print and read, if you need an interpreted string literal, you need to use double quotes.
Original string literal
What if we don't want the string to have a special format? For example, we might need to compare or calculate a computer code string that intentionally uses a backslash, so we don't want it to be used as an escape character.
originalString literals tell Go to ignore all formats in the string, including escape characters.
We use backticks to create a raw string:
(`Sammy says,\"The balloon\'s color is red.\"`)
OutputSammy says,\"The balloon\'s color is red.\"
By using backticks on both sides of a given string to construct the original string, we can preserve backslashes and other characters used as escape characters.
Summarize
This tutorial introduces several ways to format text in Go. By using techniques such as escape characters or raw strings, we are able to ensure that the program's strings are rendered correctly on the screen so that the end user can easily read all the output text.