An Introduction to String Formatting in Go

In the Go programming language, string formatting, otherwise known as string interpolation, is when you insert a custom string or variable into predefined text. It works like this:
1 2 |
a = "New Stack" "I want to say a big hello to " a "!" |
What you see above isn’t code that would work in any language but it illustrates what I mean. You have the string:
1 |
I want to say a big hello to ! |
After the word to, you want to insert a custom string (New Stack) into the pre-defined text, such that it would read:
1 |
<em>I want to say a big hello to New Stack!</em> |
The variable a is our custom string and everything else is the pre-defined text.
The Go language has this capability as well and it’s something you should learn early in your journey.
Before you learn how to format strings in Golang, you’ll need to understand the concept of printing verbs. If you’ve ever written a bash script in Linux, you might find these familiar. These verbs always start with a % character and can be used in a line of your pre-defined text.
Let’s say you have the string:
1 |
Your name is: and you are years old |
What you want to do is use string formatting to insert a name and an age into the string. For that, you could use the printing verb %s, which is the basic string printing verb, and the %d verb, which is for standing base-10 formatting (the place value system that uses decimal numbers). The above line, with the verbs added, would look something like this:
1 |
Your name is: %s, and you are %d years old. |
A sample of possible string printing verbs includes:
- %b – base-2
- %c – a character represented by its corresponding Unicode value
- %o – base-8
- %O – base-8 with the added Oo prefix
- %q – a single-quoted character
- %x – base-16
- %X – base-16 with upper-case letters
- %U – unicode format
- %b – decimal-less scientific notation with the exponent power of 2.
- %e – scientific notification
- %E – scientific notation with capital letters
- %f – decimal point without an exponent
Of course, it’s not really that simple because you have to do a bit more programming to get it to work. You’ll also need to make use of the Sprintf() function, which returns a formatted string and supports custom format specifiers. Sprintf() is found within the fmt package, so it has to be imported before your applications can use the functionality.
There’s also the printf() function. What’s the difference? Simply put, printf() sends output to stdout (standard output stream) and Sprintf() stores the formatted data in a buffer that you allocate. In our example below, we’ll use both of these functions, the first to store the string in a buffer and the second to print the buffer to standard output.
Okay, let’s format a string.
We’ll create the application as I described above, which will print out your name and age accordingly.
The first thing we’ll do is inform the Go compiler that the package should compile as an executable program. We do that with the line:
1 |
package main |
Next, we import the fmt package with:
1 |
import "fmt" |
Now, we’ll define our function with main(), which will define two variables (name and age) format the string, and then print the string. The function looks like this:
1 2 3 4 5 6 |
func main() { name := "Camille" age := 31 formatted := fmt.Sprintf("Your name is: %s, and you are %d years old", name, age) fmt.Println(formatted) } |
The := operator is used to declare a variable, which is unlike =, as that is used to change a variable’s value.
Think of := this way:
1 |
name := Jack |
is shorthand for:
1 2 |
var name int name = Jack |
Essentially := allows you to both declare and define a variable in a single line.
Now that you understand this concept, you should be able to grasp the function above.
We put the whole thing together like so:
1 2 3 4 5 6 7 8 9 10 |
package main import "fmt" func main() { name := "Camille" age := 31 formatted := fmt.Sprintf("Your name is %s, and you are %d years old", name, age) fmt.Println(formatted) } |
Create the file with the name format.go. Save and close the file. You can then run the code with the command:
1 |
go run format.go |
The output will be something like this:
1 |
Your name is Camille, and you are 31 years old |
If you want to compile that into an executable, the command would be:
1 |
go build format.go |
There’s another handy trick you might want to use with your printing verbs to format numbers. Say, for instance, you want to specify some padding for spaces in your numbers, which can help set numbers off or even allow you to print things out in columns (which, of course, requires some trickery with spacing).
Let’s say (using our example above) that you want to add some spaces before the age. When you do this, it will automatically right-justify the number and pad its left side with spaces. For example, %6d will add 6 spaces to the left of the number. That line would then look like:
1 |
formatted := fmt.Sprintf("Your name is %s, and you are %6d years old", name, age) |
What if you wanted to format that output so the name is on one line and the age is on another? For that, you could use the newline string (\n) like so:
1 |
formatted := fmt.Sprintf("Your name is %s\nYou are %d years old", name, age) |
The output of that would be:
1 2 |
Your name is Camille You are 31 years old |
Ah, the simplicity and flexibility of Golang string formatting. Once you start using this, you start to see just how important the concept is.