Rajdeep Sardar
Rajdeep Sardar

Reputation: 113

Access address of Field within Structure variable in Golang

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.

Upvotes: 2

Views: 2149

Answers (1)

peterSO
peterSO

Reputation: 166825

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

Upvotes: 5

Related Questions