An Introduction to String Formatting in Go
Learn all about the simplicity and flexibility of string formatting in the Go programming language.
Mar 19th, 2024 5:00pm by
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:
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:
I want to say a big hello to New Stack!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:
Your name is: and you are years oldWhat 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:
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
package mainNext, we import the fmt package with:
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:
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:
name := Jackis shorthand for:
var name int name = JackEssentially := 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:
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)
}
go run format.goThe output will be something like this:
Your name is Camille, and you are 31 years oldIf you want to compile that into an executable, the command would be:
go build format.goThere’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:
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:
formatted := fmt.Sprintf("Your name is %s\nYou are %d years old", name, age)
The output of that would be:
Your name is Camille You are 31 years oldAh, the simplicity and flexibility of Golang string formatting. Once you start using this, you start to see just how important the concept is.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.