Go reflect — unexported struct fields from outside the package

Contents

  1. 1. Addressable access with NewAt
  2. 2. Mutating through NewAt
  3. 3. Value (not pointer) sources
  4. 4. Mutating without a pointer to the original

Dig101: dig more, simplified more and know more

Lowercase struct fields are unexported — other packages cannot name them in ordinary code. Reflection can still reach them. Here is how reflect does it from outside the defining package.

Addressable access with NewAt

NewAt builds a Value for a pointer to a value of a given type, using p as the data pointer:

1
2
3
4
5
6
7
8
// reflect/value.go
// NewAt returns a Value representing a pointer to a value of the
// specified type, using p as that pointer.
func NewAt(typ Type, p unsafe.Pointer) Value {
fl := flag(Ptr)
t := typ.(*rtype)
return Value{t.ptrTo(), p, fl}
}

Given that, you can reflect into unexported fields. Example struct in another package:

1
2
3
4
package testData
type Example struct {
a string
}

Read field a by taking the address of eg:

1
2
3
4
5
6
7
8
9
var eg testData.Example
a:=GetStructPtrUnExportedField(&eg, "a").String()

func GetStructPtrUnExportedField(source interface{}, fieldName string) reflect.Value {
// reflect.Value for the unexported field
v := reflect.ValueOf(source).Elem().FieldByName(fieldName)
// make it addressable via NewAt
return reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
}

You must pass a pointer to source. Otherwise:

panic: reflect: call of reflect.Value.Elem on struct Value

Elem only applies when the Value is an interface or pointer — it returns the element or pointed-to value:

1
2
3
4
5
// Elem returns the value that the interface v contains
// or that the pointer v points to.
// It panics if v's Kind is not Interface or Ptr.
// It returns the zero Value if v is nil.
func (v Value) Elem() Value {

Mutating through NewAt

reflect.Value.Set (and typed setters) work on the addressable Value from NewAt, bypassing the export rule:

1
2
3
4
5
6
7
8
9
10
11
12
var eg testData.Example
err := SetStructPtrUnExportedStrField(&eg, "a", "test")

func SetStructPtrUnExportedStrField(source interface{}, fieldName string, fieldVal interface{}) (err error) {
v := GetStructPtrUnExportedField(source, fieldName)
rv := reflect.ValueOf(fieldVal)
if v.Kind() != rv.Kind() {
return fmt.Errorf("invalid kind: expected kind %v, got kind: %v", v.Kind(), rv.Kind())
}
v.Set(rv)
return nil
}

Kinds must match. Changes land on the original eg.

Value (not pointer) sources

You can start from a non-pointer value using New:

1
2
3
4
5
6
7
8
9
10
11
// New returns a Value representing a pointer to a new zero value
// for the specified type. That is, the returned Value's Type is PtrTo(typ).
func New(typ Type) Value {
if typ == nil {
panic("reflect: New(nil)")
}
t := typ.(*rtype)
ptr := unsafe_New(t)
fl := flag(Ptr)
return Value{t.ptrTo(), ptr, fl}
}

Pattern:

1
2
3
4
5
6
7
8
9
func GetStructUnExportedField(source interface{}, fieldName string) (accessableField, addressableSourceCopy reflect.Value) {
v := reflect.ValueOf(source)
// copy source so we can obtain addresses on the copy
addressableSourceCopy = reflect.New(v.Type()).Elem()
addressableSourceCopy.Set(v)
accessableField = addressableSourceCopy.FieldByName(fieldName)
accessableField = reflect.NewAt(accessableField.Type(), unsafe.Pointer(accessableField.UnsafeAddr())).Elem()
return
}

This copies the struct, then uses NewAt on the copy’s field storage.

Mutating without a pointer to the original

Reads work; writes on the copy do not update the caller’s struct — you are editing the duplicate.

Same Set helpers as above; prefer passing a pointer and NewAt when you need in-place updates.

Sample code: NewbMiao/Dig101-Go.

Questions? Leave a comment below or email: newbvirgil@gmail.com