format verb
A placeholder starting with % in a format string, such as %s or %d, that says how to print the matching argument.
In more detail
The daily ones are %s for text, %d for whole numbers, %q for quoted text, %t for booleans, %v for any value in its default form, %+v to include struct field names and %T for the type. %+v is the fastest way to inspect a struct while debugging. In fmt.Errorf, %w also wraps an error.
Examples
fmt.Printf("fields %+v\n", t)fmt.Printf("type %T\n", t)Related terms
- fmt.Printf — The fmt function that prints text built from a format string, in which verbs such as %s and %d are replaced by the arguments.
- error wrapping — Adding context to an error while keeping the original error inside it, so the message explains what failed and code can still test the cause.
- go vet — The go subcommand that looks for code that compiles but is probably wrong, such as a print format verb that does not match its argument.
Taught in
- Installing the Go Toolchain — Concurrent Systems Programming with Go
- Go Workspaces and Modules — Concurrent Systems Programming with Go
- Your First Program and go run — Concurrent Systems Programming with Go
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Structs and Methods — Concurrent Systems Programming with Go
- Errors as Values — Concurrent Systems Programming with Go
- Panics and Recover — Concurrent Systems Programming with Go
- The Standard testing Package — Concurrent Systems Programming with Go