


How Do Go's `fmt.Printf` and Pointer Types Interact, and Why Do They Produce Different Outputs for Different Pointer Types?
Dec 09, 2024 pm 08:21 PMEvaluating Differences in Go Pointers
In Go, pointers are essential for working with variables. However, understanding the nuances between different pointer types can be challenging. This article explores a specific scenario that showcases the distinctions between pointers and how they affect the output when using the default formatting in the fmt package.
The Code in Question
The following code snippet demonstrates the issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
The Issue
When executing the code, you'll notice a difference in the output when printing test2 versus test1 as a pointer. The output for test1 as a pointer is an empty string, while the output for test2 as a pointer is a hexadecimal representation of the address.
Explanation
The fmt.Printf function uses the %v verb for default formatting, which selects a specific format based on the type of value being printed. For pointers, the default format is the hexadecimal representation of the address.
In the first case (test1 as a pointer), the value being printed is a pointer to a Test struct. However, since the struct was initialized with zero values, the output is empty.
In the second case (test2 as a pointer), the value being printed is a pointer to an interface{}. Before reaching the %v verb, the value went through an additional wrapping inside another interface{}, which then points to Models["test"]. Since the value ultimately being printed is a pointer to an interface{}, the default formatting for pointers applies, and you get the hexadecimal representation of the address.
Solution
To address this issue, one needs to use type assertion to extract the actual Test struct from test2. This can be achieved by:
1 2 |
|
With the type assertion, the test2 variable now points to the Test struct, and you can print it like test1.
Alternatively, one can work with pointers to Test directly by storing *Test values in the map:
1 2 3 |
|
This approach eliminates the need for type assertion and the wrapping in interface{}, thus avoiding the hexadecimal representation of the address.
The above is the detailed content of How Do Go's `fmt.Printf` and Pointer Types Interact, and Why Do They Produce Different Outputs for Different Pointer Types?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How to convert MySQL query result List into a custom structure slice in Go language?

How do I write mock objects and stubs for testing in Go?

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications?

How to write files in Go language conveniently?
