A versatile Go library designed to output any Go variable in a structured and colored format.
This library is especially useful for debugging and testing when the standard fmt library falls short in displaying arbitrary data effectively. It can also serve as a powerful logging adapter, providing clear and readable logs for both development and production environments.
godump
is not here to replace the fmt
package. Instead, it provides an extension to what the fmt.Printf("%#v")
can do.
- ability to pretty print values of all types
- well formatted output
- unexported structs are dumped too
- pointers are followed and recursive pointers are taken in mind (see examples)
- customizable, you have full control over the output, you can even generate HTML if you'd like to, see examples
- zero dependencies
Install the library:
go get -u github.com/yassinebenaid/godump
Then use the Dump function:
package main
import (
"github.com/yassinebenaid/godump"
)
func main() {
godump.Dump("Anything")
}
If you need more control over the output. Use the Dumper
package main
import (
"os"
"github.com/yassinebenaid/godump"
)
func main() {
var v = "Foo Bar"
var d = godump.Dumper{
Indentation: " ",
HidePrivateFields: false,
Theme: godump.Theme{
String: godump.RGB{R: 138, G: 201, B: 38},
// ...
},
}
d.Print(v)
d.Println(v)
d.Fprint(os.Stdout, v)
d.Fprintln(os.Stdout, v)
d.Sprint(v)
d.Sprintln(v)
}
package main
import (
"os"
"github.com/yassinebenaid/godump"
)
func main() {
godump.Dump(os.Stdout)
}
Output:
package main
import (
"net"
"github.com/yassinebenaid/godump"
)
func main() {
godump.Dump(net.Dialer{})
}
Output:
This example shows how recursive pointers are handeled
package main
import (
"github.com/yassinebenaid/godump"
)
func main() {
type User struct {
Name string
age int
BestFriend *User
}
me := User{
Name: "yassinebenaid",
age: 22,
}
// This creates a ring
me.BestFriend = &me
godump.Dump(me)
}
Output:
This example emphasizes how you can generate html
package main
import (
"fmt"
"net"
"net/http"
"github.com/yassinebenaid/godump"
)
// Define your custome style implementation
type CSSColor struct {
R, G, B int
}
func (c CSSColor) Apply(s string) string {
return fmt.Sprintf(`<div style="color: rgb(%d, %d, %d); display: inline-block">%s</div>`, c.R, c.G, c.B, s)
}
func main() {
var d godump.Dumper
d.Theme = godump.Theme{
String: CSSColor{138, 201, 38},// edit the theme to use your implementation
Quotes: CSSColor{112, 214, 255},
Bool: CSSColor{249, 87, 56},
Number: CSSColor{10, 178, 242},
Types: CSSColor{0, 150, 199},
Address: CSSColor{205, 93, 0},
PointerTag: CSSColor{110, 110, 110},
Nil: CSSColor{219, 57, 26},
Func: CSSColor{160, 90, 220},
Fields: CSSColor{189, 176, 194},
Chan: CSSColor{195, 154, 76},
UnsafePointer: CSSColor{89, 193, 180},
Braces: CSSColor{185, 86, 86},
}
var html = `<pre style="background: #111; padding: 10px; color: white">`
html += d.Sprint(net.Dialer{})
html += "<pre>"
// render it to browsers
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, html)
})
http.ListenAndServe(":8000", nil)
}
Output:
For more examples, please have a look at dumper_test along with testdata
Please read CONTRIBUTING guildlines