How to Create Values Dynamically with reflect.New

Use reflect.New with a reflect.Type to dynamically allocate a zero-valued pointer to any Go type.

Use reflect.New with a type value to allocate a new zero-valued instance of that type as a pointer. Pass the result of reflect.TypeOf (or a stored reflect.Type) to reflect.New, then call .Interface() to get the typed pointer.

import "reflect"

// Allocate a new *MyStruct dynamically
t := reflect.TypeOf(MyStruct{})
ptr := reflect.New(t).Interface().(*MyStruct)