lundi 27 mars 2023

Get reflect.Value of nil pointer type

Using the Go reflect package I'd like to get a nil pointer of a particular type, for example I'd like a reflect.Value that represents *int with a value of nil not *0

I'm currently getting the zeroed value like this but this is *0 not nil

// varType is a reflect.Type
reflect.New(reflect.PtrTo(varType).Elem())

Will give me a zero value, e.g. *0 for *int

But what I can't quite workout is now to get a reflect.Value of say type *int and it be nil not *0

I understand why .Set(reflect.ValueOf(nil)) does not work. Perhaps I've missed a crucial step?





Aucun commentaire:

Enregistrer un commentaire