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

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

Golang: interface func to print memory address

问题

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

  1. package main
  2. import "fmt"
  3. type address struct {
  4. a int
  5. }
  6. type this interface {
  7. memory()
  8. }
  9. func (ad address) memory() {
  10. fmt.Println("a - ", ad)
  11. fmt.Println("a's memory address --> ", &ad)
  12. }
  13. func main() {
  14. ad := 43
  15. fmt.Println("a - ", ad)
  16. fmt.Println("a's memory address --> ", &ad)
  17. // 在这里初始化代码
  18. thisAddress := address{
  19. a: 42,
  20. }
  21. // 不确定为什么这里也没有返回内存地址?
  22. var i this
  23. i = thisAddress
  24. i.memory()
  25. }

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

英文:

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?

  1. package main
  2. import "fmt"
  3. type address struct {
  4. a int
  5. }
  6. type this interface {
  7. memory()
  8. }
  9. func (ad address) memory() {
  10. fmt.Println("a - ", ad)
  11. fmt.Println("a's memory address --> ", &ad)
  12. }
  13. func main() {
  14. ad := 43
  15. fmt.Println("a - ", ad)
  16. fmt.Println("a's memory address --> ", &ad)
  17. //code init in here
  18. thisAddress := address{
  19. a: 42,
  20. }
  21. // not sure why this doesnt return memory address as well?
  22. var i this
  23. i = thisAddress
  24. i.memory()
  25. }

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

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

  1. package main
  2. import "fmt"
  3. type address struct {
  4. a int
  5. }
  6. type this interface {
  7. memory() *int
  8. }
  9. func (ad address) memory() *int {
  10. /*reflect.ValueOf(&ad).Pointer() research laws of reflection */
  11. var b = &ad.a
  12. return b
  13. }
  14. func main() {
  15. thisAddress := address{
  16. a: 42,
  17. }
  18. thatAddress := address{
  19. a: 43,
  20. }
  21. var i this
  22. i = thisAddress
  23. a := i.memory()
  24. fmt.Println("I am retruned", a)
  25. fmt.Println("I am retruned", *a)
  26. i = thatAddress
  27. c := i.memory()
  28. fmt.Println("I am retruned", c)
  29. fmt.Println("I am retruned", *c)
  30. }

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

答案1

得分: 2

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

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

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

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

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

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

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

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

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

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

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

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:

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

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

  1. 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:

确定