Printf Verbs Reveal Types And Shapes
Inspect values with Printf verbs: %v and %+v for fields, %#v for Go syntax, %T for the dynamic type, %q for quoted strings, and width-precision for tables.
Code
package main
import "fmt"
type Retry struct {
Attempts int
Backoff float64
}
func main() {
r := Retry{Attempts: 3, Backoff: 0.5}
// %v 通用值;%+v 带字段名;%#v Go 语法表示
fmt.Printf("%v\n", r)
fmt.Printf("%+v\n", r)
fmt.Printf("%#v\n", r)
// %T 打印动态类型,排查接口里装了什么
var v any = r
fmt.Printf("type %T\n", v)
// %q 带引号并转义;%d/%.2f 控制数值形态;%% 输出百分号本身
fmt.Printf("msg=%q attempts=%d backoff=%.2f%%\n", "wait\tand retry", r.Attempts, r.Backoff*100)
// 宽度+精度:右对齐的表格输出
fmt.Printf("%8.2f\n%8.2f\n", 12.5, 133.257)
}How the snippet works
Printf Verbs Reveal Types And Shapes is built around the function main.
Using it
The demo output is labeled (type, msg=), so the printed output reads as a trace of the operations performed.
Key statements
r := Retry{Attempts: 3, Backoff: 0.5}
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.
FAQ
- What does Printf Verbs Reveal Types And Shapes print when run?
- Run it as a complete program — the output lines carry labels such as `type`, `msg=`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.