V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
kzzhr

分享一下自己在用的 go-dump 库

  •  
  •   kzzhr ·
    micln · Jul 19, 2020 · 2427 views
    This topic created in 2117 days ago, the information mentioned may be changed or developed.

    先看效果:

    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])
    

    https://i.imgur.com/bBV53DC.jpg

    相比 print 会提供几个信息:

    • 代码行号
    • 变量名
    • 指针类型的展开(跟 json 一样,不支持循环引用)

    为什么要搞这个东西?

    之前调 Go 的代码的时候用 print,但是打出来的日志总是不知道在哪,经常还会看到别人使用 print("===ABC===")

    后来用 GoLand 的 postfix 功能做了一键打印 json 。但脱离了 GoLand 就不太好使了,于是决定实现一个库,哪都能用。

    再发一个 postfix 的方式,在 Goland 里还是挺好用的

    https://i.imgur.com/WQaQFHq.gif

    Repo 地址(好用的话不要忘了赏我一个 star 😂)

    Github: https://github.com/Kretech/xgo/tree/master/dump

    4 replies    2020-07-21 23:54:39 +08:00
    somalia
        1
    somalia  
       Jul 19, 2020
    star
    mornlight
        2
    mornlight  
       Jul 20, 2020
    你这个获取参数名的方式有点风骚,学习了
    mornlight
        3
    mornlight  
       Jul 20, 2020
    请教一下楼主,最里面是 ast 解析源码,那么如果在编译后没有源码的运行环境,parser.ParseFile 是不是就失效了,拿不到参数名?
    kzzhr
        4
    kzzhr  
    OP
       Jul 21, 2020 via iPhone
    @mornlight 对,在没有源码的运行环境是不行的。所以主要还是用在调试。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3226 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 14:13 · PVG 22:13 · LAX 07:13 · JFK 10:13
    ♥ Do have faith in what you're doing.