lundi 16 mai 2016

How to initialize an empty pointer by reflect

This is a very straight forward question.

How do you implement Initialize() below with reflect? Or is this possible?

func Initialize(v interface{}) {
    // ... some reflection code
}

type MyType struct {
    Name string
}

func main() {
    var val *MyType

    // val is nil before initialize
    Initialize(val)
    // val is now &MyType{Name: ""}

    // ...
}

```





Aucun commentaire:

Enregistrer un commentaire