dump

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 13 Imported by: 0

README

XGo/dump

Quick Look

aInt := 1
bStr := `sf`
cMap := map[string]interface{}{"name": "z", "age": 14}
dArray := []interface{}{&cMap, aInt, bStr}

dump.Dump(aInt, &aInt, &bStr, bStr, cMap, dArray, cMap["name"], dArray[2], dArray[aInt])

Option

custom options

// disable dump in global, default false means enable
// disable it in production to ignore the unnecessary output and risks
dump.Disable = false

// show variant line in head
dump.ShowFileLine1 = true

// change writer in global
dump.DefaultWriter = os.Stdout

// only show the first-n elements
// others will show as '...'
// * these options is design for debug clearly
dump.MaxSliceLen = 32
dump.MaxMapLen   = 32

// serialize options

// render map with sorted keys
dump.OptSortMapKeys = true

// render []uint8(`ab`) as string("ab")
OptShowUint8sAsString = true

// render uint8(97) as char('a')
OptShowUint8AsChar

More TestCases

https://github.com/Kretech/xgo/blob/master/dump/dumper_test.go

https://github.com/Kretech/xgo/blob/master/dump/serialize_test.go

Documentation

Index

Examples

Constants

View Source
const (
	Zero = `<zero>`
	Nil  = "<nil>"
)

Variables

View Source
var (
	DefaultWriter io.Writer = os.Stderr

	// 显示对应代码位置
	ShowFileLine1 = true
)
View Source
var (
	// uint8(97) => 'a'
	OptShowUint8AsChar = true

	// []uint8{'a','b'} => "ab"
	OptShowUint8sAsString = true

	// 按字典序显示map.keys
	OptSortMapKeys = true
)
View Source
var (
	MaxSliceLen = 32
	MaxMapLen   = 32

	SepKv = " => "

	StringQuota = `"`
)
View Source
var Disable = false

disable dump in global scope use it in production

Functions

func Dump

func Dump(args ...interface{})
Example
package main

import (
	"os"
	"time"

	"github.com/Kretech/xgo/dump"
)

func main() {
	// just for testing
	dump.ShowFileLine1 = false
	dump.DefaultWriter = os.Stdout

	a := 1
	a2 := &a
	b := `2`
	c := map[string]interface{}{b: a}
	t1 := time.Unix(1500000000, 0)
	t2 := &t1
	s1 := []byte(`123456789012`)
	s2 := [12]byte{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76}
	s3 := &s2

	dump.Dump(a, a2, b, c, t1, t2, s1, s2, s3)
}
Output:

a => 1
a2 => *1
b => "2"
c => map[string]interface{} (len=1) {
	"2" => 1
}
t1 => time.Time 2017-07-14 02:40:00 +0000 UTC
t2 => *time.Time 2017-07-14 02:40:00 +0000 UTC
s1 => []uint8 (len=12) "123456789012"
s2 => [12]uint8 (len=12) "ABCDEFGHIJKL"
s3 => *[12]uint8 (len=12) "ABCDEFGHIJKL"

func IsScalar

func IsScalar(v interface{}) bool

IsScalar 简单类型

func Serialize added in v0.3.0

func Serialize(originValue interface{}) (serialized string)

Types

type CliDumper

type CliDumper struct {
	// contains filtered or unexported fields
}

func NewCliDumper

func NewCliDumper(X string) CliDumper
Example
package main

import (
	"os"
)

func main() {
	a := 1
	b := `2`

	ShowFileLine1 = false
	DefaultWriter = os.Stdout

	g1 := NewCliDumper(`g1`)
	g1.Dump(a, b)

	g2 := MyPackage{MyDump: MyDump}
	g2.MyDump(a, b)
}

// MyPackage mock the package name. so we can called it by `x.y()`
type MyPackage struct {
	MyDump func(...interface{})
}

func MyDump(args ...interface{}) {
	_g := NewCliDumper(`g2`) // means package name
	_g.DepthDump(1, args...)
}
Output:

a => 1
b => "2"
a => 1
b => "2"

func (CliDumper) DepthDump

func (c CliDumper) DepthDump(depth int, args ...interface{})

func (CliDumper) Dump

func (c CliDumper) Dump(args ...interface{})

Jump to

Keyboard shortcuts

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