Table of Contents
Evaluating Differences in Go Pointers
The Code in Question
The Issue
Explanation
Solution
Home Backend Development Golang How Do Go's `fmt.Printf` and Pointer Types Interact, and Why Do They Produce Different Outputs for Different Pointer Types?

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 PM

How Do Go's `fmt.Printf` and Pointer Types Interact, and Why Do They Produce Different Outputs for Different Pointer Types?

Evaluating 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

type Test struct {

    Test string

}

 

var Models = map[string]interface{}{

    "test": newTest(),

}

 

func main() {

    test1 := Test{}

    fmt.Println("Test 1: ")

    fmt.Printf("%v", test1)

    fmt.Println()

    fmt.Println("Test 1 as pointer: ")

    fmt.Printf("%v", &test1)

    fmt.Println()

    test2 := Models["test"]

    fmt.Println("Test 2: ")

    fmt.Printf("%v", test2)

    fmt.Println()

    fmt.Println("Test 2 as pointer: ")

    fmt.Printf("%v", &test2)

}

 

func newTest() Test {

    var model Test

    return model

}

Copy after login

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

t2 := Models["test"]

test2 := t2.(Test) // test2 is now of type Test

Copy after login

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

var Models = map[string]*Test{

    "test": newTest(),

}

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Go language pack import: What is the difference between underscore and without underscore? Go language pack import: What is the difference between underscore and without underscore? Mar 03, 2025 pm 05:17 PM

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 implement short-term information transfer between pages in the Beego framework? Mar 03, 2025 pm 05:22 PM

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 to convert MySQL query result List into a custom structure slice in Go language? Mar 03, 2025 pm 05:18 PM

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 do I write mock objects and stubs for testing in Go? Mar 10, 2025 pm 05:38 PM

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 define custom type constraints for generics in Go? Mar 10, 2025 pm 03:20 PM

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 can I use tracing tools to understand the execution flow of my Go applications? Mar 10, 2025 pm 05:36 PM

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

How do you write unit tests in Go? How do you write unit tests in Go? Mar 21, 2025 pm 06:34 PM

How do you write unit tests in Go?

How to write files in Go language conveniently? How to write files in Go language conveniently? Mar 03, 2025 pm 05:15 PM

How to write files in Go language conveniently?

See all articles