Documentation
¶
Overview ¶
Package pf extends fmt with pretty-printing for Go values.
Basic usage:
pf.Print(myStruct) s := pf.Sprint(myStruct) pf.Diff(oldStruct, newStruct)
Implement PrettyPrinter for custom formatting:
func (u User) PrettyPrint() string { return fmt.Sprintf("User<%s>", u.Name) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultConfig = Config{ Indent: " ", ShowTypes: false, UseJSONTags: false, MaxDepth: 0, ColorMode: true, }
DefaultConfig is the default pretty-print configuration.
Functions ¶
func FprintDiff ¶
FprintDiff writes a diff to the given writer.
Types ¶
type Config ¶
type Config struct {
// Indent string per level. Default: " "
Indent string
// ShowTypes annotates values with their type name.
ShowTypes bool
// UseJSONTags uses json tag names instead of Go field names.
UseJSONTags bool
// MaxDepth limits nesting depth (0 = unlimited).
MaxDepth int
// ColorMode enables ANSI color output.
ColorMode bool
}
Config controls pretty-print formatting.
func (Config) Print ¶
func (c Config) Print(v interface{})
Print pretty-prints to stdout using this config.
func (Config) SprintDiff ¶
SprintDiff returns a diff string using this config.
type PrettyPrinter ¶
type PrettyPrinter interface {
PrettyPrint() string
}
PrettyPrinter can be implemented by any type to control its own pretty-print output. When a value implements PrettyPrinter, pf uses PrettyPrint() instead of reflection-based formatting.
type User struct { Name string; Age int }
func (u User) PrettyPrint() string {
return fmt.Sprintf("User<%s, age=%d>", u.Name, u.Age)
}
type PrettyPrinterConfig ¶
PrettyPrinterConfig is like PrettyPrinter but receives the current Config, allowing format-aware custom output.
func (u User) PrettyPrintConfig(c pf.Config) string {
if c.ColorMode {
return "\033[36mUser<" + u.Name + ">\033[0m"
}
return "User<" + u.Name + ">"
}
Click to show internal directories.
Click to hide internal directories.