Golang结构变量中Field的访问地址 [英] Access address of Field within Structure variable in Golang

查看:144
本文介绍了Golang结构变量中Field的访问地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问GOLANG中STRUCTURE中指针或FIELD的地址。我有整个结构变量的地址或指针,但无法正确访问结构中字段的地址。到目前为止,我已经尝试了反射,但似乎打破了某处。

How to access the pointer or the address of a FIELD within a STRUCTURE in GOLANG. I have the address or pointer of the whole structure variable but can not properly access the address of the field inside structure. So far I have tried Reflection but seems breaking somewhere. Any help is highly appreciated.

推荐答案

例如,

For example,

package main

import (
    "fmt"
)

type S struct{ F1, F2 int }

func main() {
    s := new(S)
    f1, f2 := &s.F1, &s.F2
    fmt.Printf("%p %p %p\n", s, f1, f2)
}



<输出:

Output:

0x1040a128 0x1040a128 0x1040a12c

这篇关于Golang结构变量中Field的访问地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆