Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created August 6, 2020 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/ac9ae4dd54cc8a3e1e9cb7571b37405a to your computer and use it in GitHub Desktop.
Save podhmo/ac9ae4dd54cc8a3e1e9cb7571b37405a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
"unsafe"
)
type S struct {
Exported int
unexported int
}
func main() {
s := &S{Exported: 20, unexported: 10}
rv := reflect.ValueOf(s).Elem()
rf := rv.FieldByName("unexported")
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
fmt.Println(rf.Interface())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment