0
package main

import "fmt"

type Employee struct {
    ID int
    Name string
}

func main(){
    var zhexiao Employee
    zhexiao.Name = "xiao"
    fmt.Println(&zhexiao)

    x := 1
    p := &x
    fmt.Println(p)
}

The above code outputs two kinds of pointers.

  1. struct pointer output is: &{0 xiao}
  2. integer pointer output is: 0xc0420600b0 (it looks like a memory address)

Why struct pointer output is not a memory address? If it's not memory address, what is it?

Thanks a lot Zhe

2

1 Answer 1

7

It depends on how you look at it. You are implicitly using the package fmt default print verb (%v). Here are some other ways to look at it by explicitly using other print verbs.

package main

import "fmt"

type Employee struct {
    ID   int
    Name string
}

func main() {
    var zhexiao Employee
    zhexiao.Name = "xiao"
    fmt.Printf("%[1]v %[1]p\n", &zhexiao)

    x := 1
    fmt.Printf("%[1]v %[2]p\n", x, &x)
    p := &x
    fmt.Printf("%[1]v %[1]p\n", p)
}

Playground: https://play.golang.org/p/4dV8HtiS8rP

Output:

&{0 xiao} 0x1040a0d0
1 0x1041402c
0x1041402c 0x1041402c

Reference: Package fmt: Printing

Sign up to request clarification or add additional context in comments.

1 Comment

Dear, Thanks a lot. It confused me for a long time. But now i got it. LOL. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.