Difference between golang pointers

2020-02-07 06:34发布

There are 2 kinds of variables that I have. Check for the Go playground, and I don't understand why this is happening. The problem: what I get from the Models it should be a struct to use it for GORM First() function.

The code:

package main

import (
    "fmt"
)

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
}

2条回答
不美不萌又怎样
2楼-- · 2020-02-07 06:44

According to the Size and alignment guarantees as per golang docs

A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero. Two distinct zero-size variables may have the same address in memory.

This is why Test 1 is &{}

Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data.

Test2 is an interface{}, Test2 thus has pointer to information about type stored and pointer to the data itself. It Type and Value informations

查看更多
霸刀☆藐视天下
3楼-- · 2020-02-07 07:06

TL;DR: In the first case you pass a value of type *Test for printing, but in the second case you pass a value of type *interface{}! The %v verb means to format using the default formatting, but the default formatting depends on the type of the value.


The difference you see is just the default formatting rules of the fmt package's implementation.

You're using fmt.Printf():

func Printf(format string, a ...interface{}) (n int, err error)

which takes a format string and other arguments as type interface{}. So note that if the value you pass is not of type interface{}, the value will be wrapped in a value of type interface{}.

Now let's see your examples:

test1 := Test{}
// ...
fmt.Printf("%v", &test1)

test1 is of type Test, and you pass &test1 which is of type *Test. This will be wrapped in an interface{}. The formatting rules from the package doc of fmt:

For compound objects, the elements are printed using these rules, recursively, laid out like this:

struct:             {field0 field1 ...}
array, slice:       [elem0 elem1 ...]
maps:               map[key1:value1 key2:value2]
pointer to above:   &{}, &[], &map[]

Since it is a pointer to a struct, the &{} format will be used. Test has a field Test string, but you didn't set its value, so it defaults to the zero value of the type string which is the empty string "". That's why you see nothing when displayed. Note that if you would have initialized it like this:

test1 := Test{"a"}

The output would have been:

&{a}

Let's see your 2nd example:

test2 := Models["test"]
// ...
fmt.Printf("%v", &test2)

The first line is a short variable declaration, type of test2 will be inferred from the right-hand side expression. The right hand side expression is an index expression, indexing a map. Its type will be the value type of the map, and since type of Models is map[string]interface{}, type of test2 will be interface{}.

So far so good. But what happens when you try to print it like fmt.Printf("%v", &test2)? You pass a pointer to test2 which is of type interface{}, so what you pass is of type *interface{}, and since this is not identical to interface{}, it will be wrapped in another interface{} value.

So what gets passed to fmt.Printf() is an interface{} value, wrapping a *interface{} value being the address of the test2 variable.

And now the formatting rule that applies here:

The default format for %v is:

bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p

Since the value to be formatted is a pointer (*interface{}), %v will default to %p, which is:

Pointer:

%p    base 16 notation, with leading 0x

So the result is properly printing the address value in hexadecimal format, e.g.:

0x1040a160

To obtain a struct from test2, you can use type assertion. So it should rather be something like this:

t2 := Models["test"]
test2 := t2.(Test) // test2 is of type Test

This test2 has identical type to that of test1, and will produce the same result when printing. Try it on the Go Playground.

Best would be though to store *Test values in the map, and so no type assertion or even storing in local variable would be necessary, as the interface{} stored in the map would already be a pointer to Test, which can be used / passed around as-is.

查看更多
登录 后发表回答