Giter Site home page Giter Site logo

godump's Introduction

binoculars (3)

godump

Tests codecov Go Report Card OpenSSF Best Practices Version MIT License Go Reference Mentioned in Awesome Go

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)
  • customizable, you have full control over the output, you can even generate HTML if you'd like to, see examples
  • zero dependencies

Get Started

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")
}

Customization

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)
}

Demo

Example 1.

package main

import (
	"os"

	"github.com/yassinebenaid/godump"
)

func main() {
	godump.Dump(os.Stdout)
}

Output:

stdout

Example 2.

package main

import (
	"net"

	"github.com/yassinebenaid/godump"
)

func main() {
	godump.Dump(net.Dialer{})
}

Output:

dialer

Example 3.

This example shows how recursive pointers are handled.

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:

pointer

Example 4.

This example emphasizes how you can generate HTML

package main

import (
	"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(`<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:

theme

For more examples, please take a look at dumper_test along with testdata

Contribution

Please read CONTRIBUTING guidelines

godump's People

Contributors

yassinebenaid avatar ccoveille avatar dependabot[bot] avatar

Stargazers

Jalin Wang avatar Evgeny avatar  avatar Michael Schuett avatar  avatar Nathan S. Martins avatar Oleksandr Nosov avatar Wilson Gichu avatar  avatar Gabriel Chaves avatar Jarangutan avatar  avatar Amer Alimanović avatar Meir Blumenfeld avatar Stephen Afam-Osemene avatar Tom Payne avatar Brad Sickles avatar  avatar 姚文强 avatar Can Evgin avatar Shawn Zhang avatar Tiago Peczenyj avatar Lord of Scripts ™ avatar  avatar Jan-Stefan Janetzky avatar Sevdalin Sabev avatar Gonçalo Mendes Cabrita avatar Viktor Nikolaiev avatar TiX avatar Arne Van Maele avatar Edward Fernandes avatar Dan Wolf avatar  avatar Chanshik Lim avatar Brad Jones avatar Lucas Bremgartner avatar  avatar  avatar xiaodao avatar bayashi avatar Evan Godon avatar Shaun Jackson avatar Mohamad Ibrahim avatar  avatar  avatar Marcin W. Dąbrowski avatar Claus Mandal avatar  avatar Sven Hohlfeld avatar Brian Wigginton avatar Jeffrey Walter avatar  avatar xuegang.leng avatar  avatar Maciej Karpusiewicz avatar Jake Christie avatar Robert Thomas avatar John Jarvis avatar Igor Assunção avatar Adrián Robotka avatar Icaruk avatar Phillip Lorentz avatar  avatar  avatar Kwesi Rutledge avatar Tolga Atam avatar Iss M avatar  avatar alaska avatar Bjørn Wiegell avatar Alex avatar Alex Goodman avatar  avatar Scott Everitt avatar cpoul avatar isa avatar Antares avatar shengyan.cheng avatar Alexander Chereshnev avatar  avatar Marco Pantaleoni avatar Johannes Leyrer avatar Damian M. avatar Daniil Zuev avatar Khephren Barratt avatar Daniel Apatin avatar  avatar ᴀɴᴛᴏɴ ɴᴏᴠᴏᴊɪʟᴏᴠ avatar Mikhail Yeremeyev avatar Sarpdoruk Tahmaz avatar Nathaniel Sabanski avatar Dobrosław Żybort avatar Pedro avatar  avatar  avatar Zach Collier avatar Jonathan Vuillemin avatar Thijs Visser avatar Andrei Surugiu avatar Chris Tran avatar

Watchers

 avatar  avatar

godump's Issues

Show pointer values

Hi!

Looks like right now we are showing the addresses of pointers.

Pointer: *int:0x0000a4010

IMO it would be better to do something like:

Pointer: *int:&55644133

thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.