https://github.com/yassinebenaid/godump
Dump any GO variable with ease
https://github.com/yassinebenaid/godump
debug dump formatting
Last synced: 7 months ago
JSON representation
Dump any GO variable with ease
- Host: GitHub
- URL: https://github.com/yassinebenaid/godump
- Owner: yassinebenaid
- License: mit
- Created: 2024-02-18T17:12:05.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-25T09:49:15.000Z (7 months ago)
- Last Synced: 2025-04-02T03:52:59.030Z (7 months ago)
- Topics: debug, dump, formatting
- Language: Go
- Homepage:
- Size: 886 KB
- Stars: 197
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
- awesome-go - godump - Pretty print any GO variable with ease, an alternative to Go's `fmt.Printf("%#v")`. (Text Processing / Parsers/Encoders/Decoders)
- awesome-go - yassinebenaid/godump
- awesome-go - yassinebenaid/godump
README

godump
[](https://github.com/yassinebenaid/godump/actions/workflows/test.yml)
[](https://codecov.io/github/yassinebenaid/godump)
[](https://goreportcard.com/report/github.com/yassinebenaid/godump)
[](https://www.bestpractices.dev/projects/9241)
[](https://badge.fury.io/gh/yassinebenaid%2Fgodump)
[](./LICENCE)
[](https://pkg.go.dev/github.com/yassinebenaid/godump)
[](https://github.com/avelino/awesome-go?tab=readme-ov-file#parsersencodersdecoders)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.
## Why godump
- 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](#example-3))
- customizable, you have full control over the output, **you can even generate HTML if you'd like to**, [see examples](#example-4)
- zero dependencies## Get Started
Install the library:
```bash
go get -u github.com/yassinebenaid/godump
```Then use the **Dump** function:
```go
package mainimport (
"github.com/yassinebenaid/godump"
)func main() {
godump.Dump("Anything")
}```
## Customization
If you need more control over the output. Use the `Dumper`
```go
package mainimport (
"os""github.com/yassinebenaid/godump"
)func main() {
var v = "Foo Bar"
var d = godump.Dumper{
Indentation: " ",
HidePrivateFields: false,
ShowPrimitiveNamedTypes: 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)
}```
## Demo
### Example 1.
```go
package mainimport (
"os""github.com/yassinebenaid/godump"
)func main() {
godump.Dump(os.Stdout)
}```
Output:

### Example 2.
```go
package mainimport (
"net""github.com/yassinebenaid/godump"
)func main() {
godump.Dump(net.Dialer{})
}```
Output:

### Example 3.
This example shows how recursive pointers are handled.
```go
package mainimport (
"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 = &megodump.Dump(me)
}
```Output:

### Example 4.
This example emphasizes how you can generate HTML
```go
package mainimport (
"fmt"
"net"
"net/http""github.com/yassinebenaid/godump"
)// Define your custom style implementation
type CSSColor struct {
R, G, B int
}func (c CSSColor) Apply(s string) string {
return fmt.Sprintf(`%s`, 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 = `
`
html += d.Sprint(net.Dialer{})
html += ""// 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 take a look at [dumper_test](./dumper_test.go) along with [testdata](./testdata)
## Contribution
Please read [CONTRIBUTING guidelines](.github/CONTRIBUTING.md)