When working with the Printf()
function in Go, you’ll have access to a variety of formatting verbs that allow you to control how data is presented.
General Formatting Verbs
The following are formatting verbs applicable to all kinds of data types:
Verb | Description |
---|---|
%v | Displays the value in the default format. |
%#v | Outputs the value in Go-syntax format (ideal for debugging). |
%T | Presents the type of the value. |
%% | Prints out a percent sign. |
Example Code:
package main import ("fmt") func main() { var speed = 120.75 var brand = "Ducati" fmt.Printf("%v\n", speed) fmt.Printf("%#v\n", speed) fmt.Printf("%v%%\n", speed) fmt.Printf("%T\n", speed) fmt.Printf("%v\n", brand) fmt.Printf("%#v\n", brand) fmt.Printf("%T\n", brand) }
Output:
120.75 120.75 120.75% float64 Ducati "Ducati" string
Integer Formatting Verbs
These formatting verbs work exclusively with integers:
Verb | Description |
---|---|
%b | Displays the binary representation (base 2). |
%d | Shows the decimal representation (base 10). |
%+d | Shows the decimal representation including the sign. |
%o | Displays the value in octal format (base 8). |
%O | Octal format with a leading 0o . |
%x | Outputs the hexadecimal value (lowercase). |
%X | Outputs the hexadecimal value (uppercase). |
%#x | Hexadecimal format with a leading 0x . |
%4d | Pads spaces to make it width 4, right-aligned. |
%-4d | Pads spaces to make it width 4, left-aligned. |
%04d | Pads zeroes to make it width 4. |
Example Code:
package main import ("fmt") func main() { var maxSpeed = 150 fmt.Printf("%b\n", maxSpeed) fmt.Printf("%d\n", maxSpeed) fmt.Printf("%+d\n", maxSpeed) fmt.Printf("%o\n", maxSpeed) fmt.Printf("%O\n", maxSpeed) fmt.Printf("%x\n", maxSpeed) fmt.Printf("%X\n", maxSpeed) fmt.Printf("%#x\n", maxSpeed) fmt.Printf("%4d\n", maxSpeed) fmt.Printf("%-4d\n", maxSpeed) fmt.Printf("%04d\n", maxSpeed) }
Output:
10010110 150 +150 226 0o226 96 96 0x96 150 150 0150
String Formatting Verbs
For string data types, here are the options:
Verb | Description |
---|---|
%s | Displays the string as it is. |
%q | Wraps the string in double quotes. |
%8s | Adjusts the width to 8, right-aligned. |
%-8s | Adjusts the width to 8, left-aligned. |
%x | Converts the string into hexadecimal format. |
% x | Converts the string into hexadecimal format with spaces included. |
Example Code:
package main import ("fmt") func main() { var motorcycleBrand = "Harley" fmt.Printf("%s\n", motorcycleBrand) fmt.Printf("%q\n", motorcycleBrand) fmt.Printf("%8s\n", motorcycleBrand) fmt.Printf("%-8s\n", motorcycleBrand) fmt.Printf("%x\n", motorcycleBrand) fmt.Printf("% x\n", motorcycleBrand) }
Output:
Harley "Harley" Harley Harley 4861726c6579 48 61 72 6c 65 79
Boolean Formatting Verbs
Boolean values have a straightforward formatting option:
Verb | Description |
---|---|
%t | Displays the value as true or false . |
Example Code:
package main import ("fmt") func main() { var hasFuel = true var needsService = false fmt.Printf("%t\n", hasFuel) fmt.Printf("%t\n", needsService) }
Output:
true false
Float Formatting Verbs
Here are formatting verbs that deal with floating-point numbers:
Verb | Description |
---|---|
%e | Shows the number in scientific notation with ‘e’. |
%f | Displays the number with a decimal point but without an exponent. |
%.2f | Limits the precision to 2 decimal points. |
%6.2f | Adjusts the width to 6 and precision to 2. |
%g | Uses the exponent only if necessary. |
Example Code:
package main import ("fmt") func main() { var mileage = 25.768 fmt.Printf("%e\n", mileage) fmt.Printf("%f\n", mileage) fmt.Printf("%.2f\n", mileage) fmt.Printf("%6.2f\n", mileage) fmt.Printf("%g\n", mileage) }
Output:
2.576800e+01 25.768000 25.77 25.77 25.768