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.
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.
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.
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:
`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.
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|
|t||Tab (horizontal indentation)|
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!"")
OutputSammy 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:
fmt.Println("This stringnspans multiplenlines.")
OutputThis 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:
Output1. 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.
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".
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.
` 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:
OutputThis 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 onn" + "multiple linesn" + "within three singlen" + "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.
A raw string literal tells Go to ignore all formatting within a string, including escape characters.
We create a raw string by using back quotes around the string:
fmt.Println(`Sammy says,"The balloon's color is red."`)
OutputSammy 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.