How can I do this? I want a function to return a variable with the same type as one of its arguments. I need something like the below:
type Whatever struct {
Title string
}
hey:= Whatever{Title:"YAY"}
thetype := reflect.ValueOf(hey).Kind()
// This does not work
BB:= new(thetype)
If you want to create a new value from a reflect.Type
you can do it with reflect.New
:
thetype := reflect.TypeOf(hey)
BB:= reflect.New(thetype)
This returns a reflect.Value
You can then for example use .Interface()
and type assertions to get back to the original type.
Example on Go playground: https://play.golang.org/p/rL-Hm0IUpd