-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (115 loc) · 2.92 KB
/
main.go
File metadata and controls
135 lines (115 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//go:build ignore
// +build ignore
package main
import (
"fmt"
"os"
"strings"
"github.com/goforj/godump"
)
type Profile struct {
Age int
Email string
}
type User struct {
Name string
Profile Profile
}
// main demonstrates the builder-style API.
func main() {
user := User{
Name: "Alice",
Profile: Profile{
Age: 30,
Email: "alice@example.com",
},
}
// Basic pretty-print
godump.Dump(user)
// #main.User {
// +Name => "Alice" #string
// +Profile => #main.Profile {
// +Age => 30 #int
// +Email => "alice@example.com" #string
// }
// }
// Dump as string
strOut := godump.DumpStr(user)
fmt.Println("DumpStr:", strOut)
// DumpStr output:
// #main.User {
// +Name => "Alice" #string
// +Profile => #main.Profile {
// +Age => 30 #int
// +Email => "alice@example.com" #string
// }
// }
// Dump as HTML
htmlOut := godump.DumpHTML(user)
fmt.Println("DumpHTML:", htmlOut)
// <pre class="godump">…formatted HTML output…</pre>
// Dump JSON
godump.DumpJSON(user)
// {"Name":"Alice","Profile":{"Age":30,"Email":"alice@example.com"}}
// Dump to any io.Writer
godump.Fdump(os.Stderr, user)
// (same output as Dump but written to stderr)
// Dump and exit
// godump.Dd(user)
// (prints formatted dump then immediately exits)
// -------------------------------------------------
// Custom Dumper (Builder API)
// -------------------------------------------------
d := godump.NewDumper(
godump.WithMaxDepth(15),
godump.WithMaxItems(100),
godump.WithMaxStringLen(100000),
godump.WithWriter(os.Stdout),
godump.WithSkipStackFrames(10),
godump.WithDisableStringer(false),
godump.WithoutColor(),
)
// Using the custom dumper
d.Dump(user)
// #main.User {
// +Name => "Alice" #string
// +Profile => #main.Profile {
// +Age => 30 #int
// +Email => "alice@example.com" #string
// }
// }
// Dump to string using custom dumper
out := d.DumpStr(user)
fmt.Println("Custom DumpStr:\n", out)
// Custom DumpStr:
// #main.User {
// +Name => "Alice" #string
// +Profile => #main.Profile {
// +Age => 30 #int
// +Email => "alice@example.com" #string
// }
// }
// Dump to HTML
html := d.DumpHTML(user)
fmt.Println("Custom DumpHTML:\n", html)
// <pre class="godump">…formatted HTML output…</pre>
// JSON as string
jsonStr := d.DumpJSONStr(user)
fmt.Println("Custom JSON:\n", jsonStr)
// {"Name":"Alice","Profile":{"Age":30,"Email":"alice@example.com"}}
// Print JSON directly
d.DumpJSON(user)
// {"Name":"Alice","Profile":{"Age":30,"Email":"alice@example.com"}}
// Dump to a strings.Builder
var sb strings.Builder
custom := godump.NewDumper(godump.WithWriter(&sb))
custom.Dump(user)
fmt.Println("Dump to strings.Builder:\n", sb.String())
// #main.User {
// +Name => "Alice" #string
// +Profile => #main.Profile {
// +Age => 30 #int
// +Email => "alice@example.com" #string
// }
// }
}