Use reflect.Type.Elem()
:
s := (*SomeType)(nil) t := reflect.TypeOf(s).Elem() v := reflect.New(t) sp := (*SomeType)(unsafe.Pointer(v.Pointer())) sp.A = 3
Playground: http://play.golang.org/p/Qq8eo-W2yq
EDIT : Elwinar in the comments below indicated that you can get the structure without unsafe.Pointer
using reflect.Indirect()
:
s := (*SomeType)(nil) t := reflect.TypeOf(s).Elem() ss := reflect.Indirect(reflect.New(t)).Interface().(SomeType) ss.A = 3
Playground: http://play.golang.org/p/z5xgEMR_Vx
source share