Common Verb Types
General Verbs
%v (Default Value Representation)
package main
import "fmt"
func main() {
data := struct {
Name string
Age int
}{"LabEx", 5}
fmt.Printf("Default: %v\n", data)
fmt.Printf("Detailed: %+v\n", data)
fmt.Printf("Go Syntax: %#v\n", data)
}
func main() {
x := 42
str := "Hello"
fmt.Printf("Integer Type: %T\n", x)
fmt.Printf("String Type: %T\n", str)
}
Numeric Verbs
Verb |
Description |
Example |
%d |
Decimal integer |
42 |
%b |
Binary representation |
101010 |
%x |
Hexadecimal |
2a |
%o |
Octal |
52 |
func main() {
num := 42
fmt.Printf("Decimal: %d\n", num)
fmt.Printf("Binary: %b\n", num)
fmt.Printf("Hex: %x\n", num)
fmt.Printf("Octal: %o\n", num)
}
Floating-Point Verbs
graph LR
A[Floating-Point Verbs]
A --> B[%f Standard Notation]
A --> C[%e Scientific Notation]
A --> D[%g Compact Representation]
func main() {
pi := 3.14159
fmt.Printf("Standard: %f\n", pi) // 3.141590
fmt.Printf("Precision: %.2f\n", pi) // 3.14
fmt.Printf("Scientific: %e\n", pi) // 3.141590e+00
fmt.Printf("Compact: %g\n", pi) // 3.14159
}
String Verbs
%s (Basic String)
%q (Quoted String)
func main() {
msg := "Hello, LabEx!"
fmt.Printf("Normal: %s\n", msg)
fmt.Printf("Quoted: %q\n", msg)
}
Boolean and Pointer Verbs
func main() {
flag := true
ptr := &flag
fmt.Printf("Boolean: %t\n", flag)
fmt.Printf("Pointer: %p\n", ptr)
}
- Width Specification
- Precision Control
- Alignment Options
func main() {
fmt.Printf("Padded: %5d\n", 42) // Right-aligned
fmt.Printf("Left-Padded: %-5d\n", 42) // Left-aligned
fmt.Printf("Precision: %.2f\n", 3.14159)
}