dump

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 12 Imported by: 61

README

Dump

goutil/dump is a golang data printing toolkit that prints beautiful and easy to read go slice, map, struct data

Install

go get github.com/gookit/goutil/dump

Go docs

Usage

run demo: go run ./dump/_examples/demo1.go

package main

import "github.com/gookit/goutil/dump"

// rum demo: go run ./dump/_examples/demo1.go
func main() {
	otherFunc1()
}

func otherFunc1() {
	dump.P(
		23,
		[]string{"ab", "cd"},
		[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // len > 10
		map[string]interface{}{
			"key": "val", "sub": map[string]string{"k": "v"},
		},
		struct {
			ab string
			Cd int
		}{
			"ab", 23,
		},
	)
}

You will see:

More preview

  • print struct

  • nested struct

Functions API

func Clear(vs ...interface{})
func Config(fn func(opts *Options))
func Format(vs ...interface{}) string
func Fprint(w io.Writer, vs ...interface{})
func NoLoc(vs ...interface{})
func P(vs ...interface{})
func Print(vs ...interface{})
func Println(vs ...interface{})
func Reset()
func V(vs ...interface{})
type Dumper struct{ ... }
    func NewDumper(out io.Writer, skip int) *Dumper
    func NewWithOptions(fn func(opts *Options)) *Dumper
    func Std() *Dumper
type Options struct{ ... }
    func NewDefaultOptions(out io.Writer, skip int) *Options

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./timex/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./timex/...

Documentation

Overview

Package dump like fmt.Println but more pretty and beautiful print Go values.

Index

Examples

Constants

View Source
const (
	Fnopos = 1 << iota // no position
	Ffunc
	Ffile
	Ffname
	Fline
)

These flags define which print caller information

Variables

This section is empty.

Functions

func Clear added in v0.5.0

func Clear(vs ...any)

Clear dump clear data, without location.

func Config

func Config(fn func(opts *Options))

Config std dumper

func Format added in v0.4.5

func Format(vs ...any) string

Format like fmt.Println, but the output is clearer and more beautiful

func Fprint

func Fprint(w io.Writer, vs ...any)

Fprint like fmt.Println, but the output is clearer and more beautiful

func NoLoc added in v0.5.0

func NoLoc(vs ...any)

NoLoc dump vars data, without location.

func P

func P(vs ...any)

P like fmt.Println, but the output is clearer and more beautiful

func Print

func Print(vs ...any)

Print like fmt.Println, but the output is clearer and more beautiful

Example
Config(func(d *Options) {
	d.NoColor = true
})
defer Reset()

Print(
	23,
	[]string{"ab", "cd"},
	[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
	map[string]string{"key": "val"},
	map[string]any{
		"sub": map[string]string{"k": "v"},
	},
	struct {
		ab string
		Cd int
	}{
		"ab", 23,
	},
)

// Output like:
// PRINT AT github.com/gookit/goutil/dump.ExamplePrint(LINE 14):
// int(23)
// []string{"ab", "cd"}
// []int [
//   1,
//   2,
//   3,
//   4,
//   5,
//   6,
//   7,
//   8,
//   9,
//   10,
//   11,
// ]
// map[string]string {
//   key: "val",
// }
// map[string]interface {} {
//   sub: map[string]string{"k":"v"},
// }
// struct { ab string; Cd int } {
//   ab: "ab",
//   Cd: 23,
// }
//
Output:

func Println

func Println(vs ...any)

Println like fmt.Println, but the output is clearer and more beautiful

func Reset added in v0.3.0

func Reset()

Reset std dumper

func V

func V(vs ...any)

V like fmt.Println, but the output is clearer and more beautiful

Types

type Dumper added in v0.3.0

type Dumper struct {
	*Options
	// contains filtered or unexported fields
}

Dumper struct definition

func NewDumper added in v0.3.0

func NewDumper(out io.Writer, skip int) *Dumper

NewDumper create

func NewWithOptions added in v0.3.14

func NewWithOptions(fn func(opts *Options)) *Dumper

NewWithOptions create

func Std added in v0.3.0

func Std() *Dumper

Std dumper

func (*Dumper) Dump added in v0.3.0

func (d *Dumper) Dump(vs ...any)

Dump vars

func (*Dumper) Fprint added in v0.3.0

func (d *Dumper) Fprint(w io.Writer, vs ...any)

Fprint print vars to io.Writer

func (*Dumper) Print added in v0.3.0

func (d *Dumper) Print(vs ...any)

Print vars. alias of Dump()

func (*Dumper) Println added in v0.3.0

func (d *Dumper) Println(vs ...any)

Println vars. alias of Dump()

func (*Dumper) ResetOptions added in v0.3.0

func (d *Dumper) ResetOptions()

ResetOptions for dumper

func (*Dumper) WithOptions added in v0.3.0

func (d *Dumper) WithOptions(fn func(opts *Options)) *Dumper

WithOptions for dumper

func (*Dumper) WithSkip added in v0.3.0

func (d *Dumper) WithSkip(skip int) *Dumper

WithSkip for dumper

func (*Dumper) WithoutColor added in v0.3.10

func (d *Dumper) WithoutColor() *Dumper

WithoutColor for dumper

type Options added in v0.3.0

type Options struct {
	// Output the output writer
	Output io.Writer
	// NoType dont show data type TODO
	NoType bool
	// NoColor don't with color
	NoColor bool
	// IndentLen width. default is 2
	IndentLen int
	// IndentChar default is one space
	IndentChar byte
	// MaxDepth for nested print
	MaxDepth int
	// ShowFlag for display caller position
	ShowFlag int
	// MoreLenNL array/slice elements length > MoreLenNL, will wrap new line
	// MoreLenNL int
	// CallerSkip skip for call runtime.Caller()
	CallerSkip int
	// ColorTheme for print result.
	ColorTheme Theme
}

Options for dump vars

func NewDefaultOptions added in v0.3.10

func NewDefaultOptions(out io.Writer, skip int) *Options

NewDefaultOptions create.

type Theme added in v0.3.10

type Theme map[string]string

Theme color code/tag map for dump

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL