我如何打印(到控制台)的Id,标题,名称等,这个结构在Golang?
type Project struct {
Id int64 `json:"project_id"`
Title string `json:"title"`
Name string `json:"name"`
Data Data `json:"data"`
Commits Commits `json:"commits"`
}
我如何打印(到控制台)的Id,标题,名称等,这个结构在Golang?
type Project struct {
Id int64 `json:"project_id"`
Title string `json:"title"`
Name string `json:"name"`
Data Data `json:"data"`
Commits Commits `json:"commits"`
}
当前回答
我推荐使用Pretty Printer Library。这样你可以很容易地打印任何结构体。
安装库 https://github.com/kr/pretty
or
go get github.com/kr/pretty
现在在代码中这样做
package main
import (
fmt
github.com/kr/pretty
)
func main(){
type Project struct {
Id int64 `json:"project_id"`
Title string `json:"title"`
Name string `json:"name"`
Data Data `json:"data"`
Commits Commits `json:"commits"`
}
fmt.Printf("%# v", pretty.Formatter(Project)) //It will print all struct details
fmt.Printf("%# v", pretty.Formatter(Project.Id)) //It will print component one by one.
}
此外,您还可以通过这个库获取组件之间的差异等等。你也可以在这里查看图书馆文档。
其他回答
还有go-render,它处理指针递归和字符串和int映射的大量键排序。
安装:
go get github.com/luci/go-render/render
例子:
type customType int
type testStruct struct {
S string
V *map[string]int
I interface{}
}
a := testStruct{
S: "hello",
V: &map[string]int{"foo": 0, "bar": 1},
I: customType(42),
}
fmt.Println("Render test:")
fmt.Printf("fmt.Printf: %#v\n", a)))
fmt.Printf("render.Render: %s\n", Render(a))
打印:
fmt.Printf: render.testStruct{S:"hello", V:(*map[string]int)(0x600dd065), I:42}
render.Render: render.testStruct{S:"hello", V:(*map[string]int){"bar":1, "foo":0}, I:render.customType(42)}
p = Project{...}
fmt.Printf("%+v", p)
fmt.Printf("%#v", p) //with type
使用包fmt输出非常方便:
fmt.Printf("%+v \n", yourProject)
如果你想看到完整的sturct类型,你可以使用# replace +:
fmt.Printf("%#v \n", yourProject)
或者,尝试使用这个函数PrettyPrint()
// print the contents of the obj
func PrettyPrint(data interface{}) {
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}
为了使用这个,除了fmt和encoding/json之外,你不需要任何额外的包,只需要一个引用、指针或你所创建的结构的文字。
为了使用你的结构,初始化它在main或任何包中,并将它传递给PrettyPrint()。
type Prefix struct {
Network string
Mask int
}
func valueStruct() {
// struct as a value
var nw Prefix
nw.Network = "10.1.1.0"
nw.Mask = 24
fmt.Println("### struct as a pointer ###")
PrettyPrint(&nw)
}
它的输出是
### struct as a pointer ###
{
"Network": "10.1.1.0",
"Mask": 24
}
摆弄一下这里的代码。
打印结构中字段的名称:
fmt.Printf("%+v\n", yourProject)
从fmt包:
打印结构体时,加号标记(%+v)添加字段名
假设你有一个项目的实例(在'yourProject'中)
文章JSON和Go将详细介绍如何从JSON结构中检索值。
这个Go by示例页面提供了另一种技术:
type Response2 struct {
Page int `json:"page"`
Fruits []string `json:"fruits"`
}
res2D := &Response2{
Page: 1,
Fruits: []string{"apple", "peach", "pear"}}
res2B, _ := json.Marshal(res2D)
fmt.Println(string(res2B))
这将打印:
{"page":1,"fruits":["apple","peach","pear"]}
如果您没有任何实例,那么您需要使用反射来显示给定结构的字段名称,如本例所示。
type T struct {
A int
B string
}
t := T{23, "skidoo"}
s := reflect.ValueOf(&t).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}