Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golang-用于打印复杂数据结构的库 gop #797

Open
guanhui07 opened this issue Oct 4, 2022 · 0 comments
Open

golang-用于打印复杂数据结构的库 gop #797

guanhui07 opened this issue Oct 4, 2022 · 0 comments
Labels

Comments

@guanhui07
Copy link
Owner

guanhui07 commented Oct 4, 2022

项目地址 https://github.com/ysmood/got/tree/master/lib/gop

目的是把复杂的结构体打印成易于阅读的格式。输出的格式使用的 Go 原生语法,不需学习任何额外的知识就可以阅读输出。不像其他库可能需要考猜测输出格式。

特性
使用原生 Go 语法输出
支持循环引用(比如 go-spew 的这个 bug, 而 gop 可以正常打印)
更好的 rune, []byte, time 等数据的可读性
字符串里的隐藏字符也会被打印
可配置的高亮打印
根据排序后的 key 输出一致的 map
自动将多行大段文字打印成多行以便于阅读
可扩展的 API
用法
通常你只需要用 gop.P 函数:

package main

import (
    "time"

    "github.com/ysmood/got/lib/gop"
)

func main() {
    val := map[string]interface{}{
        "bool":   true,
        "number": 1 + 1i,
        "bytes":  []byte{97, 98, 99},
        "lines":  "multiline string\nline two",
        "slice":  []interface{}{1, 2},
        "time":   time.Now(),
        "struct": struct{ test int32 }{
            test: 13,
        },
    }
    val["slice"].([]interface{})[1] = val["slice"]

    gop.P(val)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant