打印时如何取消对字段的引用? [英] How to dereference fields when printing?
本文介绍了打印时如何取消对字段的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
http://play.golang.org/p/joEmjQdMaS
package main
import "fmt"
type SomeStruct struct {
somePointer *somePointer
}
type somePointer struct {
field string
}
func main() {
fmt.Println(SomeStruct{&somePointer{"I want to see what is in here"}})
}
这会打印一个像这样的内存地址{0x10500168}
This prints a memory address like this {0x10500168}
有没有办法打印它?
{{"I want to see what is in here"}}
这主要是出于调试目的,如果我有一个带有30个指针字段的结构,我不想为这30个字段中的每一个都做一个println来查看其中的内容.
This is mostly for debugging purposes, if I had a struct with 30 pointer fields, I didn't want to have to do a println for each of the 30 fields to see what is in it.
推荐答案
有一个很棒的软件包,叫做 go-spew .正是您想要的.
There is a great package called go-spew. Does exactly what you want.
package main
import (
"github.com/davecgh/go-spew/spew"
)
type (
SomeStruct struct {
Field1 string
Field2 int
Field3 *somePointer
}
somePointer struct {
field string
}
)
func main() {
s := SomeStruct{
Field1: "Yahoo",
Field2: 500,
Field3: &somePointer{"I want to see what is in here"},
}
spew.Dump(s)
}
将为您提供此输出:
(main.SomeStruct) {
Field1: (string) "Yahoo",
Field2: (int) 500,
Field3: (*main.somePointer)(0x2102a7230)({
field: (string) "I want to see what is in here"
})
}
这篇关于打印时如何取消对字段的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文