package godump
Import Path
github.com/goforj/godump (on go.dev)
Dependency Relation
imports 11 packages, and imported by one package
Package-Level Type Names (total 3)
Colorizer is a function type that takes a color code and a string, returning the colorized string.
Dumper holds configuration for dumping structured data.
It controls depth, item count, and string length limits.
Dd is a debug function that prints the values and exits the program.
@group Debug
Example: dump and exit with a custom dumper
d := godump.NewDumper()
v := map[string]int{"a": 1}
d.Dd(v)
// #map[string]int {
// a => 1 #int
// }
Diff prints a diff between two values to the configured writer.
@group Diff
Example: print diff with a custom dumper
d := godump.NewDumper()
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
d.Diff(a, b)
// <#diff // path:line
// - #map[string]int {
// - a => 1 #int
// - }
// + #map[string]int {
// + a => 2 #int
// + }
DiffHTML returns an HTML diff between two values.
@group Diff
Example: HTML diff with a custom dumper
d := godump.NewDumper()
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
html := d.DiffHTML(a, b)
_ = html
// (html diff)
DiffStr returns a string diff between two values.
@group Diff
Example: diff string with a custom dumper
d := godump.NewDumper()
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
out := d.DiffStr(a, b)
_ = out
// <#diff // path:line
// - #map[string]int {
// - a => 1 #int
// - }
// + #map[string]int {
// + a => 2 #int
// + }
Dump prints the values to stdout with colorized output.
@group Dump
Example: print with a custom dumper
d := godump.NewDumper()
v := map[string]int{"a": 1}
d.Dump(v)
// #map[string]int {
// a => 1 #int
// }
DumpHTML dumps the values as HTML with colorized output.
@group HTML
Example: dump HTML with a custom dumper
d := godump.NewDumper()
v := map[string]int{"a": 1}
html := d.DumpHTML(v)
_ = html
fmt.Println(html)
// (html output)
DumpJSON prints a pretty-printed JSON string to the configured writer.
@group JSON
Example: print JSON
v := map[string]int{"a": 1}
d := godump.NewDumper()
d.DumpJSON(v)
// {
// "a": 1
// }
DumpJSONStr pretty-prints values as JSON and returns it as a string.
@group JSON
Example: dump JSON string
v := map[string]int{"a": 1}
d := godump.NewDumper()
out := d.DumpJSONStr(v)
_ = out
// {"a":1}
DumpStr returns a string representation of the values with colorized output.
@group Dump
Example: get a string dump with a custom dumper
d := godump.NewDumper()
v := map[string]int{"a": 1}
out := d.DumpStr(v)
_ = out
// "#map[string]int {\n a => 1 #int\n}" #string
func NewDumper(opts ...Option) *Dumper
Option defines a functional option for configuring a Dumper.
func WithDisableStringer(b bool) Option
func WithMaxDepth(n int) Option
func WithMaxItems(n int) Option
func WithMaxStringLen(n int) Option
func WithoutColor() Option
func WithSkipStackFrames(n int) Option
func WithWriter(w io.Writer) Option
func NewDumper(opts ...Option) *Dumper
Package-Level Functions (total 18)
Dd is a debug function that prints the values and exits the program.
@group Dump
Example: dump and exit
v := map[string]int{"a": 1}
godump.Dd(v)
// #map[string]int {
// a => 1 #int
// }
Diff prints a diff between two values to stdout.
@group Diff
Example: print diff
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
godump.Diff(a, b)
// <#diff // path:line
// - #map[string]int {
// - a => 1 #int
// - }
// + #map[string]int {
// + a => 2 #int
// + }
DiffHTML returns an HTML diff between two values.
@group Diff
Example: HTML diff
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
html := godump.DiffHTML(a, b)
_ = html
// (html diff)
DiffStr returns a string diff between two values.
@group Diff
Example: diff string
a := map[string]int{"a": 1}
b := map[string]int{"a": 2}
out := godump.DiffStr(a, b)
_ = out
// <#diff // path:line
// - #map[string]int {
// - a => 1 #int
// - }
// + #map[string]int {
// + a => 2 #int
// + }
Dump prints the values to stdout with colorized output.
@group Dump
Example: print to stdout
v := map[string]int{"a": 1}
godump.Dump(v)
// #map[string]int {
// a => 1 #int
// }
DumpHTML dumps the values as HTML with colorized output.
@group HTML
Example: dump HTML
v := map[string]int{"a": 1}
html := godump.DumpHTML(v)
_ = html
// (html output)
DumpJSON dumps the values as a pretty-printed JSON string.
If there is more than one value, they are dumped as a JSON array.
It returns an error string if marshaling fails.
@group JSON
Example: print JSON
v := map[string]int{"a": 1}
godump.DumpJSON(v)
// {
// "a": 1
// }
DumpJSONStr dumps the values as a JSON string.
@group JSON
Example: JSON string
v := map[string]int{"a": 1}
out := godump.DumpJSONStr(v)
_ = out
// {"a":1}
DumpStr returns a string representation of the values with colorized output.
@group Dump
Example: get a string dump
v := map[string]int{"a": 1}
out := godump.DumpStr(v)
godump.Dump(out)
// "#map[string]int {\n a => 1 #int\n}" #string
Fdump writes the formatted dump of values to the given io.Writer.
@group Dump
Example: dump to writer
var b strings.Builder
v := map[string]int{"a": 1}
godump.Fdump(&b, v)
// outputs to strings builder
NewDumper creates a new Dumper with the given options applied.
Defaults are used for any setting not overridden.
@group Builder
Example: build a custom dumper
v := map[string]int{"a": 1}
d := godump.NewDumper(
godump.WithMaxDepth(10),
godump.WithWriter(os.Stdout),
)
d.Dump(v)
// #map[string]int {
// a => 1 #int
// }
WithDisableStringer disables using the fmt.Stringer output.
When enabled, the underlying type is rendered instead of String().
@group Options
Example: show raw types
// Default: false
v := time.Duration(3)
d := godump.NewDumper(godump.WithDisableStringer(true))
d.Dump(v)
// 3 #time.Duration
WithMaxDepth limits how deep the structure will be dumped.
Param n must be 0 or greater or this will be ignored, and default MaxDepth will be 15.
@group Options
Example: limit depth
// Default: 15
v := map[string]map[string]int{"a": {"b": 1}}
d := godump.NewDumper(godump.WithMaxDepth(1))
d.Dump(v)
// #map[string]int {
// a => #map[string]int {
// b => ... (max depth)
// }
// }
WithMaxItems limits how many items from an array, slice, or map can be printed.
Param n must be 0 or greater or this will be ignored, and default MaxItems will be 100.
@group Options
Example: limit items
// Default: 100
v := []int{1, 2, 3}
d := godump.NewDumper(godump.WithMaxItems(2))
d.Dump(v)
// #[]int [
// 0 => 1 #int
// 1 => 2 #int
// ... (truncated)
// ]
WithMaxStringLen limits how long printed strings can be.
Param n must be 0 or greater or this will be ignored, and default MaxStringLen will be 100000.
@group Options
Example: limit string length
// Default: 100000
v := "hello world"
d := godump.NewDumper(godump.WithMaxStringLen(5))
d.Dump(v)
// "hello…" #string
WithoutColor disables colorized output for the dumper.
@group Options
Example: disable colors
// Default: false
v := map[string]int{"a": 1}
d := godump.NewDumper(godump.WithoutColor())
d.Dump(v)
// (prints without color)
// #map[string]int {
// a => 1 #int
// }
WithSkipStackFrames skips additional stack frames for header reporting.
This is useful when godump is wrapped and the actual call site is deeper.
@group Options
Example: skip wrapper frames
// Default: 0
v := map[string]int{"a": 1}
d := godump.NewDumper(godump.WithSkipStackFrames(2))
d.Dump(v)
// <#dump // ../../../../usr/local/go/src/runtime/asm_arm64.s:1223
// #map[string]int {
// a => 1 #int
// }
WithWriter routes output to the provided writer.
@group Options
Example: write to buffer
// Default: stdout
var b strings.Builder
v := map[string]int{"a": 1}
d := godump.NewDumper(godump.WithWriter(&b))
d.Dump(v)
// #map[string]int {
// a => 1 #int
// }
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |