How to Print the Values of Nested Structs with Pointers in Go?
Printing Struct Values with Pointers in Go
In Go, it's common to encounter situations where you need to print the value of a struct that contains pointers to other structs. However, the default behavior of the %v format specifier in fmt.Printf() displays the pointer address instead of the actual value.
Consider the following example:
package main import "fmt" type A struct { a int32 B *B } type B struct { b int32 } func main() { a := &A{ a: 1, B: &B{ b: 2, }, } fmt.Printf("v ==== %+v \n", a) }
The output of the above code is:
v ==== &{a:1 B:0xc42000e204}
As you can see, the B field is printed as the memory address of the B struct, not its actual value.
Custom Printing with Stringer Interface
One way to print the contents of nested structs is to implement the Stringer interface for both the A and B types. The Stringer interface requires a single method, String(), which returns a string representation of the value.
Here's an updated example with the Stringer interface:
package main import "fmt" type A struct { a int32 B *B } type B struct{ b int32 } func (aa *A) String() string { return fmt.Sprintf("A{a:%d, B:%v}",aa.a,aa.B) } func (bb *B) String() string { return fmt.Sprintf("B{b:%d}",bb.b) } func main() { a := &A{a: 1, B: &B{b: 2}} // using the Stringer interface fmt.Printf("v ==== %v \n", a) // or just print it yourself however you want. fmt.Printf("v ==== A{a:%d, B:B{b:%d}}\n", a.a, a.B.b) // or just reference the values in the struct that are structs themselves // but this can get really deep fmt.Printf("v ==== A{a:%d, B:%v}", a.a, a.B) }
Now, the output is:
v ==== A{a:1, B:B{b:2}}
Manual Printing
If you don't wish to implement the Stringer interface, you can manually print the desired representation of the struct using the print statements. For example, you can access the nested struct's fields and print them separately:
fmt.Printf("v ==== A{a:%d, B:B{b:%d}}\n", a.a, a.B.b)
This approach gives you complete control over the format of the output.
The above is the detailed content of How to Print the Values of Nested Structs with Pointers in Go?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
