fmt.Printf
is like the Swiss Army knife of Go when it comes to string formatting. Whether you’re debugging, logging, or trying to impress your peers with some beautifully formatted output, fmt.Printf
has your back. So, let’s dive into some of the most commonly used formatting verbs and flags in Go’s fmt
package.
The Basic Placeholders
Here’s the bread and butter of fmt.Printf
—the simple formatting verbs:
-
%v: Prints the value in a default format. It’s like the wild card of formatting.
fmt.Printf("%v\n", 42) // 42 fmt.Printf("%v\n", true) // true fmt.Printf("%v\n", [2]int{1, 2}) // [1 2]
-
%T: Prints the type of the variable.
fmt.Printf("%T\n", 42) // int fmt.Printf("%T\n", 3.14) // float64
-
%%: Literally prints a percent sign. Super useful when you’re not trying to divide by 100.
fmt.Printf("Discount: 50%%\n") // Discount: 50%
Numbers: Because Math Matters
Handling numbers? Go’s got you covered with specific verbs for various number types:
-
%d: For integers, printed in base 10 (decimal).
fmt.Printf("%d\n", 255) // 255
-
%b: Prints an integer in binary. Great for those moments you want to feel like a computer.
fmt.Printf("%b\n", 255) // 11111111
-
%x and %X: For hexadecimal output.
%x
uses lowercase letters, and%X
uses uppercase.fmt.Printf("%x\n", 255) // ff fmt.Printf("%X\n", 255) // FF
-
%f, %e, and %g: For floating-point numbers.
%f
prints a float in decimal, while%e
uses scientific notation.fmt.Printf("%f\n", 3.14159) // 3.141590 fmt.Printf("%e\n", 3.14159) // 3.141590e+00
Strings and Runes: Getting Texty
When you’re working with strings and characters:
-
%s: Prints a string.
fmt.Printf("%s\n", "GoLang") // GoLang
-
%q: Double-quoted string. Adds quotes around your string automatically—useful when you’re debugging and want to spot those pesky spaces.
fmt.Printf("%q\n", "GoLang") // "GoLang"
-
%c: Prints a character, or more technically, a rune.
fmt.Printf("%c\n", 65) // A
Width and Precision: It’s All About Presentation
fmt.Printf
also gives you control over the width and precision of your output, because why not format numbers like an art piece?
-
%5d: Pads the number with spaces so it takes up 5 characters. Numbers less than 5 characters will be right-aligned.
fmt.Printf("%5d\n", 42) // 42
-
%.2f: Specifies the number of digits after the decimal point for floats.
fmt.Printf("%.2f\n", 3.14159) // 3.14
Flags: Fine-Tuning the Output
There are also handy flags you can throw into the mix for even more control:
-
+: Forces a sign (
+
or-
) for numeric values.fmt.Printf("%+d\n", 42) // +42
-
0: Pads numbers with leading zeros instead of spaces.
fmt.Printf("%05d\n", 42) // 00042
-
#: Adds prefixes for certain bases (like
0x
for hex or0b
for binary).fmt.Printf("%#x\n", 255) // 0xff
Bonus Round: Sprintf and Friends
If you want the formatted string instead of printing it directly, use fmt.Sprintf
. It’s like Printf
, but without the commitment to standard output.
s := fmt.Sprintf("Pi to two decimals: %.2f", 3.14159)
fmt.Println(s) // Pi to two decimals: 3.14
Mastering fmt.Printf
will make your Go programs not only functional but also look sharp in the terminal. You’ll be surprised at how often good formatting makes your life easier, whether it’s logging, debugging, or preparing console output for the masses. So, next time you’re printing, do it with style!