打印指针变量

I created 2 Vertex objects below- q & q.

Now, when I print the pointer variable q = &Vertex, I expected it to be the memory address, why is it printing the - &{1,2}

Output:

{1 2} &{1 2}

Program:

package main

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    p = Vertex{1, 2}  // has type Vertex
    q = &Vertex{1, 2} // has type *Vertex
)

func main() {
    fmt.Println(p, q)
}

Playground

From https://golang.org/pkg/fmt/ :

Println formats using the default formats for its operands

If you want to print in a specific way, you need to use fmt.Printf() and supply the format you want.

The fmt.Println(...) function "[uses] the default formats for its operands" and according to the fmt package header documentation:

%v  the value in a default format
...
struct:             {field0 field1 ...}
...
pointer to above:   &{}, &[], &map[]

So the following lines are effectively the same:

fmt.Println(p, q)
fmt.Printf("%v %v
", p, q)

If you want to print the memory address of a pointer then you should use the %p format verb:

Pointer:
%p  base 16 notation, with leading 0x

For example:

fmt.Printf("%p
", q) // => 0x1953e4