Learning Golang: Printf

Share on:

This is part 16 of my journey learning Golang.

The Printf method

The Printf method from the fmt package writes a formatted string to standard output.

Printf makes it possible to interpolate strings. This is done by leaving placeholders in a string, and using values to fill in these placeholders.

Printf syntax

Printf accepts a string argument, potentially with formatting placeholders, and zero or more value arguments. For example:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  answer := "C"
 9  fmt.Printf("Is %v your final answer?", answer)
10}

prints out:

1Is C your final answer?

Keep in mind that, like Print, Printf does not automatically add a newline character.

The %v portion of the first argument is the placeholder for a “value”.

Multiple placeholders

The “format specifier” i.e. the first argument can contain an arbitrary number of placeholders. This way, multiple values can be printed out with a single statement. This is one use case where Printf shines over Print. To achieve the same thing with Print several statements would be required. Additionally Print doesn’t support any formatting other than the default string representation of its argument.

For example:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  const name, age, pronoun, profession = "Kim", 22, "She", "woodworker"
 9  fmt.Printf("%s is %d years old.\n", name, age)
10  fmt.Printf("%s is a %s.\n", pronoun, profession)
11}

prints out:

1Kim is 22 years old.
2She is a woodworker.

A newline character is printed after each of these lines, because it was explicitly included in the format specifier.

Notice that the placeholders used were %s (for string) and %d (for decimal i.e. base 10 integer number).

Verbs

The placeholders in the format specifier are called “verbs”. To view a full list of supported format verbs, see the fmt package’s documentation.

These are some of the most common ones:

Verb Meaning
%v the value in a default format
%+v a default representation of structs, with field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%d an integer value in decimal (base 10) representation
%E scientific notation, e.g. -1.234456E+78
%f decimal point but no exponent, e.g. 123.456
%s a string or slice (without interpreting the bytes)
%q a double-quoted string safely escaped with Go syntax

To print a literal % (percent) symbol escape it like this: %%.

Width and precision

The width of each formatted value can be specified by an optional decimal number immediately preceding the verb.

The precision can be specified after the (optional) width by a period followed by a decimal number.

This example shows some common uses of these modifiers:

 1package main
 2
 3import (
 4  "fmt"
 5)
 6
 7func main() {
 8  const s, n = "Pi", 3.1415926535
 9  fmt.Printf("'%v'='%v'\n", s, n)
10  fmt.Printf("'%s'='%f'\n", s, n)
11  fmt.Printf("'%10s'='%1.4f'\n", s, n)
12  fmt.Printf("'%-10s'='%1.1f'\n", s, n)
13}

this is the output:

1'Pi'='3.1415926535'
2'Pi'='3.141593'
3'        Pi'='3.1416'
4'Pi        '='3.1'

Note that in the example above the single quotes (') are just regular characters to help visualize where each formatted value starts and ends. They have no special meaning for the Printf method.

There are many more special cases and flags than would fit an introductory article. The package’s documentation is a great place to find out what all the possibilities are.

Takeaways

Printf is an extremely useful and flexible method. It resembles in versatility the original printf function of the C language, but it is simpler to use.

Do you often use Printf? What other tips would you highlight?


comments powered by Disqus