gookit/goutil/dump - 是一个golang数据打印工具包,能够打印出丑陋易读的go slice, map, struct数据。

次要性能:

  • 应用简略,间接调用 dump.P(vars...) 即可
  • 反对所有的根底数据类型
  • 反对slice, map, struct数据结构
  • 反对传入打印多个变量
  • 默认输入调用地位,方便使用
  • 反对自定义局部能力,如 缩进,色调主题等

成果预览:

Git Repo:
  • Gitee: https://gitee.com/gookit/goutil
  • Github: https://github.com/gookit/goutil/
  • GoDoc: https://pkg.go.dev/github.com/gookit/goutil/dump

打印根底类型

package mainimport "github.com/gookit/goutil/dump"// rum demo://     go run ./dump/_examples/basic_types.gofunc main() {    dump.P(        nil, true,        12, int8(12), int16(12), int32(12), int64(12),        uint(22), uint8(22), uint16(22), uint32(22), uint64(22),        float32(23.78), float64(56.45),        'c', byte('d'),        "string",    )}

输入成果:

打印slice

打印 array, slice 都会一行一个元素输入,同时会在最初输入长度。

package mainimport "github.com/gookit/goutil/dump"// rum demo://     go run ./dump/_examples/slice.gofunc main() {    dump.P(        []byte("abc"),        []int{1, 2, 3},        []string{"ab", "cd"},        []interface{}{            "ab",            234,            []int{1, 3},            []string{"ab", "cd"},        },    )}

输入成果:

打印map

打印map数据结构,会一行一个元素输入,同时会在最初输入map长度。

package mainimport "github.com/gookit/goutil/dump"// rum demo://     go run ./map.go//     go run ./dump/_examples/map.gofunc main() {    dump.P(        map[string]interface{}{            "key0": 123,            "key1": "value1",            "key2": []int{1, 2, 3},            "key3": map[string]string{                "k0": "v0",                "k1": "v1",            },        },    )}

输入成果:

打印struct

打印struct数据,指针类型会主动打印底层实在数据

package mainimport (    "fmt"    "github.com/gookit/color"    "github.com/gookit/goutil/dump")// rum demo://     go run ./struct.go//     go run ./dump/_examples/struct.gofunc main() {    s1 := &struct {        cannotExport map[string]interface{}    }{        cannotExport: map[string]interface{}{            "key1": 12,            "key2": "abcd123",        },    }    s2 := struct {        ab string        Cd int    }{        "ab", 23,    }    color.Infoln("- Use fmt.Println:")    fmt.Println(s1, s2)    color.Infoln("\n- Use dump.Println:")    dump.P(        s1,        s2,    )}

输入成果:

自定义dumper

反对自定义dumper一些选项。如 缩进,色调主题等

// Options for dump varstype 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}

Git Repo

  • Gitee: https://gitee.com/gookit/goutil
  • Github: https://github.com/gookit/goutil
  • GoDoc: https://pkg.go.dev/github.com/gookit/goutil/dump