3

goutil/dump - 打印漂亮易读的go数据

 2 years ago
source link: https://studygolang.com/articles/35472
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

goutil/dump - 打印漂亮易读的go数据

inhere · 2天之前 · 216 次点击 · 预计阅读时间 4 分钟 · 大约8小时之前 开始浏览    

gookit/goutil/dump - 是一个golang数据打印工具包,可以打印出漂亮易读的go slice, map, struct数据。

主要功能:

  • 使用简单,直接调用 dump.P(vars...) 即可
  • 支持所有的基础数据类型
  • 支持slice, map, struct数据结构
  • 支持传入打印多个变量
  • 默认输出调用位置,方便使用
  • 支持自定义部分能力,如 缩进,色彩主题等

效果预览:

image.png

打印基础类型

package main

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

// rum demo:
//     go run ./dump/_examples/basic_types.go
func 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",
    )
}

输出效果:

image

打印slice

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

package main

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

// rum demo:
//     go run ./dump/_examples/slice.go
func main() {
    dump.P(
        []byte("abc"),
        []int{1, 2, 3},
        []string{"ab", "cd"},
        []interface{}{
            "ab",
            234,
            []int{1, 3},
            []string{"ab", "cd"},
        },
    )
}

输出效果:

image

打印map

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

package main

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

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

输出效果:

image

打印struct

打印struct数据,指针类型会自动打印底层真实数据

package main

import (
    "fmt"

    "github.com/gookit/color"
    "github.com/gookit/goutil/dump"
)

// rum demo:
//     go run ./struct.go
//     go run ./dump/_examples/struct.go
func 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,
    )
}

输出效果:

image

自定义dumper

支持自定义dumper一些选项。如 缩进,色彩主题等

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

Git Repo


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK