Golang:接口函数打印内存地址。

huangapple go评论98阅读模式
英文:

Golang: interface func to print memory address

问题

我很好奇为什么直接在变量上打印内存地址可以正常工作,但是通过接口进行相同操作时,却无法打印出内存地址。

package main

import "fmt"

type address struct {
    a int
}

type this interface {
    memory()
}

func (ad address) memory() {
    fmt.Println("a - ", ad)
    fmt.Println("a's memory address --> ", &ad)
}

func main() {
    ad := 43
    fmt.Println("a - ", ad)
    fmt.Println("a's memory address --> ", &ad)

    // 在这里初始化代码
    thisAddress := address{
        a: 42,
    }
    // 不确定为什么这里也没有返回内存地址?
    var i this
    i = thisAddress
    i.memory()
}

我只返回翻译好的部分,不包括其他内容。

英文:

I am curious as to why just printing the memory address on a var directly works but trying to do the same action through an interface doesn't print out the memory address?

package main

import "fmt"

type address struct {
	a int
}

type this interface {
	memory()
}

func (ad address) memory() {
	fmt.Println("a - ", ad)
	fmt.Println("a's memory address --> ", &ad)
}

func main() {
	ad := 43
	fmt.Println("a - ", ad)
	fmt.Println("a's memory address --> ", &ad)

	//code init in here
	thisAddress := address{
		a: 42,
	}
	// not sure why this doesnt return memory address as well?
	var i this
	i = thisAddress
	i.memory()
}

https://play.golang.org/p/Ko8sEVfehv

Just wanted to add this after fixing errors, it now functions as expected.
Testing shifting memory pointers

package main

import "fmt"

type address struct {
  a int
}

type this interface {
  memory() *int
}

func (ad address) memory() *int {

  /*reflect.ValueOf(&ad).Pointer() research laws of reflection */
  var b = &ad.a

  return b
}

func main() {



  thisAddress := address{
	  a: 42,
  }
  thatAddress := address{
  	  a: 43,
  }

  var i this
  i = thisAddress
  a := i.memory()

  fmt.Println("I am retruned", a)
  fmt.Println("I am retruned", *a)
  i = thatAddress
  c := i.memory()
  fmt.Println("I am retruned", c)
  fmt.Println("I am retruned", *c)
}

https://play.golang.org/p/BnB14-yX8B

答案1

得分: 2

因为在memory()方法的第二个案例中:

func (ad address) memory() {
    fmt.Println("a - ", ad)
    fmt.Println("a's memory address --> ", &ad)
}

ad不是一个int,而是一个结构体,ad的类型是address。你打印的不是一个int的地址,而是一个结构体的地址。指向结构体的指针的默认格式是:&{}

引用fmt包文档中关于默认格式的说明:

> 结构体: {字段0 字段1 ...}
> 数组、切片: [元素0 元素1 ...]
> 映射: map[键1:值1 键2:值2]
> 指向上述类型的指针: &{}, &[], &map[]

如果你修改这一行代码,打印address.a字段的地址,该字段的类型是int

fmt.Println("a's memory address --> ", &ad.a)

你将看到相同的指针格式以十六进制格式打印出来,例如:

a's memory address -->  0x1040e13c
英文:

Because in your second case inside the memory() method:

func (ad address) memory() {
    fmt.Println("a - ", ad)
    fmt.Println("a's memory address --> ", &ad)
}

ad is not an int but a struct, ad is of type address. And you're not printing the address of an int but an address of a struct. And the default formatting for pointer to a struct is: &{}.

Quoting from package doc of fmt regarding default formats:

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

If you modify the line to print the address of the address.a field which is of type int:

fmt.Println("a's memory address --> ", &ad.a)

You will see the same pointer format printed in hexadecimal format, e.g.:

a's memory address -->  0x1040e13c

huangapple
  • 本文由 发表于 2016年9月28日 14:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/39739743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定