Welcome to The Coding College! Formatting verbs in Go (Golang) are essential for creating structured and dynamic output. By using verbs with the fmt
package, you can customize how data is displayed, ensuring precision and clarity.
What Are Formatting Verbs?
Formatting verbs are placeholders used in fmt
functions like fmt.Printf
and fmt.Sprintf
. They specify how values should be formatted for output.
Commonly Used Formatting Verbs
1. General Verbs
These verbs apply to most data types:
Verb | Description | Example Output |
---|---|---|
%v | Default format for the value | fmt.Printf("%v", 42) → 42 |
%+v | Adds field names (structs) | fmt.Printf("%+v", person) |
%#v | Go-syntax representation | fmt.Printf("%#v", person) |
%T | Type of the value | fmt.Printf("%T", 42) → int |
%% | Literal percent sign | fmt.Printf("%%") → % |
2. Integer Verbs
Use these for formatting integers:
Verb | Description | Example Output |
---|---|---|
%d | Decimal (base 10) | fmt.Printf("%d", 42) → 42 |
%b | Binary | fmt.Printf("%b", 5) → 101 |
%o | Octal | fmt.Printf("%o", 9) → 11 |
%x | Hexadecimal (lowercase) | fmt.Printf("%x", 15) → f |
%X | Hexadecimal (uppercase) | fmt.Printf("%X", 15) → F |
%c | Unicode character | fmt.Printf("%c", 65) → A |
%U | Unicode format | fmt.Printf("%U", 65) → U+0041 |
3. Floating-Point and Complex Verbs
These are used for floating-point numbers and complex values:
Verb | Description | Example Output |
---|---|---|
%f | Decimal format | fmt.Printf("%f", 3.14) → 3.140000 |
%.nf | Specify decimal places (precision) | fmt.Printf("%.2f", 3.14) → 3.14 |
%e | Scientific notation (lowercase) | fmt.Printf("%e", 3.14) → 3.140000e+00 |
%E | Scientific notation (uppercase) | fmt.Printf("%E", 3.14) → 3.140000E+00 |
%g | Compact format (scientific/decimal) | fmt.Printf("%g", 3.14) → 3.14 |
4. String and Byte Slice Verbs
These apply to strings and byte slices:
Verb | Description | Example Output |
---|---|---|
%s | String | fmt.Printf("%s", "Go") → Go |
%q | Quoted string | fmt.Printf("%q", "Go") → "Go" |
%x | Hex dump (lowercase) | fmt.Printf("%x", "Go") → 476f |
%X | Hex dump (uppercase) | fmt.Printf("%X", "Go") → 476F |
5. Pointer Verbs
Pointers can also be formatted:
Verb | Description | Example Output |
---|---|---|
%p | Pointer address | fmt.Printf("%p", &x) → 0xc0000100a0 |
Controlling Width and Alignment
You can specify field width and alignment with numbers in the format string:
1. Width
- Minimum width for a field:
fmt.Printf("%10s", "Go") // Right-align
Output:
Go
2. Left-Align
- Use
-
to left-align:
fmt.Printf("%-10s is great!", "Go")
Output:
Go is great!
3. Width and Precision for Floats
- Specify both width and precision:
fmt.Printf("%10.2f", 3.14159)
Output:
3.14
Practical Examples
Example 1: Format Various Types
package main
import "fmt"
func main() {
var name = "Alice"
var age = 30
var height = 5.75
fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height)
}
Output:
Name: Alice, Age: 30, Height: 5.75
Example 2: Inspect Type and Value
package main
import "fmt"
func main() {
var x = 42
fmt.Printf("Value: %v, Type: %T\n", x, x)
}
Output:
Value: 42, Type: int
Example 3: Enumerate Integers
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
fmt.Printf("Decimal: %d, Binary: %b, Hex: %x\n", i, i, i)
}
}
Output:
Decimal: 1, Binary: 1, Hex: 1
Decimal: 2, Binary: 10, Hex: 2
Decimal: 3, Binary: 11, Hex: 3
Decimal: 4, Binary: 100, Hex: 4
Decimal: 5, Binary: 101, Hex: 5
Best Practices
- Choose the Right Verb: Ensure the verb matches the data type.
- Use Precision for Floats: Avoid unnecessary decimals with
%.nf
. - Combine Verbs for Structs: Use
%+v
for detailed output of structs. - Inspect Types with
%T
: Debug by checking variable types.
Conclusion
Formatting verbs in Go provide powerful tools for customizing output, making your programs more user-friendly and professional. By mastering these verbs, you can write better code that communicates effectively.