Golang:用于打印内存地址的接口函数 [英] Golang: interface func to print memory address
问题描述
我很好奇为什么只是在var上打印内存地址直接工作,但试图通过一个接口执行相同的操作不会打印出内存地址?
package main
导入fmt
类型地址struct {
a int
}
键入这个接口{
memory()
}
func(广告地址)memory(){
fmt.Println(a - ,ad)
fmt.Println(a的内存地址 - >和&ad)
}
func main(){
ad: = 43
fmt.Println(a - ,ad)
fmt.Println(a的内存地址 - >和&ad)
//代码init在这里
thisAddress:=地址{
a:42,
}
//不知道为什么这不会返回内存地址?
var i this
i = thisAddress
i.memory()
}
https://play.golang.org/p/Ko8sEVfehv
只是想在修复错误后添加它,现在按预期运行。
测试移位内存指针
package main
导入fmt
类型地址struct {
a int
}
键入此接口{
memory()* int
}
func(广告地址)内存()* int {
/*reflect.ValueOf(& ad).Pointer()研究反射法则* /
var b =& ad.a
返回b
}
func main(){
thisAddress:=地址{
a:42,
}
thatAddress:=地址{
a:43,
}
var i this
i = thisAddress
a:= i.memory()
fmt.Println(我重造了,a)
fmt.Println(我重造了,* a)
i = thatAddress
c:= i.memory()
fmt.Println(我重造了,c)
fmt.Println(我重造了,* c)
}
https://play.golang.org/p/BnB14-yX8B
< pre $
func(广告地址)内存(){
fmt.Println(a - ,ad)
fmt.Println(a的内存地址 - > ; ,& ad)
}
ad
不是 int
,但是一个结构体, ad
的类型是 address
。而且您不打印 int
的地址,而是打印 struct
。而指向结构体的默认格式是:& {}
。
有关默认格式的 fmt
:
struct:{field0 field1 ...}
array,slice:[elem0 elem1 ... ]
maps:map [key1:value1 key2:value2]
指向上面的指针:& {},& [],& map []
如果您修改该行以打印 address.a $ c的地址$ c>字段,其类型
int
:
fmt.Println (a的内存地址 - >,& ad.a)
您将看到以十六进制格式打印的相同指针格式,例如:
a的内存地址 - > 0x1040e13c
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
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
这篇关于Golang:用于打印内存地址的接口函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!