英文:
How to dereference fields when printing?
问题
以下是翻译好的内容:
package main
import "fmt"
type SomeStruct struct {
somePointer *somePointer
}
type somePointer struct {
field string
}
func main() {
fmt.Println(SomeStruct{&somePointer{"我想看看这里面是什么"}})
}
这段代码会打印出一个内存地址,类似于 {0x10500168}。
有没有办法让它打印出:
{{"我想看看这里面是什么"}}
这主要是为了调试目的,如果我有一个包含30个指针字段的结构体,我不想为每个字段都打印一行来查看其中的内容。
英文:
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"}})
}
This prints a memory address like this {0x10500168}
Is there a way to make it print:
{{"I want to see what is in here"}}
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.
答案1
得分: 23
有一个很棒的包叫做go-spew,它完全符合你的需求。
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{"我想看看这里面有什么"},
}
spew.Dump(s)
}
将会得到以下输出:
(main.SomeStruct) {
Field1: (string) "Yahoo",
Field2: (int) 500,
Field3: (*main.somePointer)(0x2102a7230)({
field: (string) "我想看看这里面有什么"
})
}
英文:
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)
}
Will give you this output:
(main.SomeStruct) {
Field1: (string) "Yahoo",
Field2: (int) 500,
Field3: (*main.somePointer)(0x2102a7230)({
field: (string) "I want to see what is in here"
})
}
答案2
得分: 3
以下是您要翻译的内容:
package main
import (
"fmt"
)
type SomeTest struct {
someVal string
}
func (this *SomeTest) String() string {
return this.someVal
}
func main() {
fmt.Println(&SomeTest{"You can see this now"})
}
任何实现了Stringer接口的对象都可以通过其String()方法进行打印。要实现Stringer接口,只需实现String() string方法。要实现您想要的功能,您需要为SomeStruct实现Stringer接口(在您的情况下,解引用somePointer并对其进行操作)。
英文:
package main
import (
"fmt"
)
type SomeTest struct {
someVal string
}
func (this *SomeTest) String() string {
return this.someVal
}
func main() {
fmt.Println(&SomeTest{"You can see this now"})
}
Anything that provides the Stringer interface will be printed with it's String() method. To implement stringer, you only need to implement String() string. To do what you want, you'd have to implement Stringer for SomeStruct (in your case, dereference somePointer and do something with that).
答案3
得分: 2
你正在尝试打印一个包含指针的结构体。当你打印结构体时,它会打印包含的类型的值 - 在这种情况下是字符串指针的值。
你不能在结构体内部解引用字符串指针,因为这样它就不再准确地由结构体描述,而且你也不能解引用结构体,因为它不是指针。
你可以解引用字符串指针,但不能在结构体内部进行解引用。
func main() {
pointer := SomeStruct{&somePointer{"我想看看这里面是什么"}}.somePointer
fmt.Println(*pointer)
}
输出:`{我想看看这里面是什么}`
你也可以直接从Println中打印特定的值:
```go
func main() {
fmt.Println(SomeStruct{&somePointer{"我想看看这里面是什么"}}.somePointer)
}
输出:`&{我想看看这里面是什么}`
另一种尝试的方法是使用Printf:
```go
func main() {
structInstance := SomeStruct{&somePointer{"我想看看这里面是什么"}}
fmt.Printf("%s", structInstance)
}
输出:`{%!s(*main.somePointer=&{我想看看这里面是什么})}`
英文:
You're attempting to print a struct that contains a pointer. When you print the struct, it's going to print the values of the types contained - in this case the pointer value of a string pointer.
You can't dereference the string pointer within the struct because then it's no longer accurately described by the struct and you can't dereference the struct because it's not a pointer.
What you can do is dereference the string pointer, but not from within the struct.
func main() {
pointer := SomeStruct{&somePointer{"I want to see what is in here"}}.somePointer
fmt.Println(*pointer)
}
output: {I want to see what is in here}
You can also just print the specific value from within the Println:
func main() {
fmt.Println(SomeStruct{&somePointer{"I want to see what is in here"}}.somePointer)
}
output: &{I want to see what is in here}
Another thing to try is Printf:
func main() {
structInstance := SomeStruct{&somePointer{"I want to see what is in here"}}
fmt.Printf("%s",structInstance)
}
output: {%!s(*main.somePointer=&{I want to see what is in here})}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论