Go实战--golang中使用go-spew(davecgh/go-spew) - Go语言中文社区

Go实战--golang中使用go-spew(davecgh/go-spew)


生命不止,继续 go go go !!!

花絮:
一系列的事儿,开始想想未来如何,要不要离开北京。利用周末,去了趟南京,感觉很好。
我的csdn博客uv访问量如下:
这里写图片描述

—————————————————-悲伤的分割线————————————————–

今天跟大家一起分享一个golang的第三方库go-spew。

go-spew

Implements a deep pretty printer for Go data structures to aid in debugging

简介:
Go-spew implements a deep pretty printer for Go data structures to aid in debugging. A comprehensive suite of tests with 100% test coverage is provided to ensure proper functionality. See test_coverage.txt for the gocov coverage report. Go-spew is licensed under the liberal ISC license, so it may be used in open source or commercial projects.

github地址:
https://github.com/davecgh/go-spew

Star: 1688

文档地址:
https://godoc.org/github.com/davecgh/go-spew/spew

获取:
go get -u github.com/davecgh/go-spew/spew

dump a variable:

spew.Dump(myVar1, myVar2, ...)
spew.Fdump(someWriter, myVar1, myVar2, ...)
str := spew.Sdump(myVar1, myVar2, ...)

应用

spew.Dump

package main

import (
    "github.com/davecgh/go-spew/spew"
)

type Project struct {
    Id      int64  `json:"project_id"`
    Title   string `json:"title"`
    Name    string `json:"name"`
    Data    string `json:"data"`
    Commits string `json:"commits"`
}

func main() {

    o := Project{Name: "hello", Title: "world"}
    spew.Dump(o)
}

输出:

(main.Project) {
 Id: (int64) 0,
 Title: (string) (len=5) "world",
 Name: (string) (len=5) "hello",
 Data: (string) "",
 Commits: (string) ""
}

spew.Printf

package main

import (
    "github.com/davecgh/go-spew/spew"
)

func main() {

    ui8 := uint8(5)
    pui8 := &ui8
    ppui8 := &pui8

    // Create a circular data type.
    type circular struct {
        ui8 uint8
        c   *circular
    }
    c := circular{ui8: 1}
    c.c = &c

    // Print!
    spew.Printf("ppui8: %vn", ppui8)
    spew.Printf("circular: %vn", c)
}

输出:

ppui8: <**>5
circular: {1 <*>{1 <*><shown>}}

spew.ConfigState

package main

import (
    "github.com/davecgh/go-spew/spew"
)

func main() {

    scs := spew.ConfigState{Indent: "t"}

    // Output using the ConfigState instance.
    v := map[string]int{"one": 1}
    scs.Printf("v: %vn", v)
    scs.Dump(v)
}

输出:

v: map[one:1]
(map[string]int) (len=1) {
    (string) (len=3) "one": (int) 1
}

这里写图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wangshubo1989/article/details/78674248
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-27 19:30:15
  • 阅读 ( 1728 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢