Formatting Verbs for Printf()
- Go provides a variety of formatting verbs that can be used with the Printf() function to format and display output in a structured way. These verbs control how different types of data are printed.
General Formatting Verbs in Go
- These formatting verbs can be used with any data type in Go:
Verb Description
%v Displays the value using the default format
%#v Displays the value in Go syntax format
%T Prints the data type of the value
%% Prints a literal percent sign (%)
Example:
package main
func main() {
number := 15.5
message := "Hello World!"
fmt.Printf("Default format: %v\n", number)
fmt.Printf("Go-syntax format: %#v\n", number)
fmt.Printf("With percentage: %v%%\n", number)
fmt.Printf("Type of number: %T\n", number)
fmt.Printf("Default format: %v\n", message)
fmt.Printf("Go-syntax format: %#v\n", message)
fmt.Printf("Type of message: %T\n", message)
}
Output:
Default format: 15.5
Go-syntax format: 15.5
With percentage: 15.5%
Type of number: float64
Default format: Hello World!
Go-syntax format: "Hello World!"
Type of message: string
Integer Formatting Verbs in Go
- The following formatting verbs are used with the Printf() function to format integer values in various ways:
Verb Description
%b Binary (base 2)
%d Decimal (base 10)
%+d Decimal with sign (always show sign)
%o Octal (base 8)
%O Octal with leading 0o
%x Hexadecimal (base 16, lowercase)
%X Hexadecimal (base 16, uppercase)
%#x Hexadecimal with leading 0x
%4d Decimal, width 4 (right-aligned, padded with spaces)
%-4d Decimal, width 4 (left-aligned, padded with spaces)
%04d Decimal, width 4 (padded with leading zeros)
Example:
package main
func main() {
var i = 15
fmt.Printf("Binary : %b\n", i)
fmt.Printf("Decimal : %d\n", i)
fmt.Printf("Signed : %+d\n", i)
fmt.Printf("Octal : %o\n", i)
fmt.Printf("Octal (0o) : %O\n", i)
fmt.Printf("Hex (lower) : %x\n", i)
fmt.Printf("Hex (upper) : %X\n", i)
fmt.Printf("Hex (0x) : %#x\n", i)
fmt.Printf("Width 4 : %4d\n", i)
fmt.Printf("Left Align : %-4d\n", i)
fmt.Printf("Zero Padded : %04d\n", i)
}
Output:
Binary : 1111
Decimal : 15
Signed : +15
Octal : 17
Octal (0o) : 0o17
Hex (lower) : f
Hex (upper) : F
Hex (0x) : 0xf
Width 4 : 15
Left Align : 15
Zero Padded : 0015
String Formatting Verbs in Go
- The Printf() function in Go supports various formatting verbs for working with string values. Below are commonly used verbs for string formatting:
Verb Description
%q Prints the value as a double-quoted string
%8s Prints the string right-justified with width 8
%-8s Prints the string left-justified with width 8
%x Prints the string as a hexadecimal dump of byte values
% x Prints the hex dump with spaces between byte values
Example:
package main
import ("fmt")
func main() {
var txt = "Hello"
fmt.Printf("%s\n", txt)
fmt.Printf("%q\n", txt)
fmt.Printf("%8s\n", txt)
fmt.Printf("%-8s\n", txt)
fmt.Printf("%x\n", txt)
fmt.Printf("% x\n", txt)
}
Output:
Hello
"Hello"
Hello
Hello
48656c6c6f
48 65 6c 6c 6f
Boolean Formatting Verbs
- In Go, the following formatting verb is used with boolean data types:
Verb Description
%t Prints the boolean value as true or false (equivalent to %v)
Example:
package main
import (
"fmt"
)
func main() {
var i = true
var j = false
fmt.Printf("%t\n", i)
fmt.Printf("%t\n", j)
}
Output:
true
false
Float Formatting Verbs
- The following formatting verbs can be used with float data types in Go: