mardi 28 mars 2017

reflect.New returns

I am using reflection for a library I'm building but there's something I don't understand about reflect.New.

type A struct {
    A int
    B string
}

func main() {

    real := new(A)
    reflected := reflect.New(reflect.TypeOf(real)).Elem().Interface()
    fmt.Println(real)
    fmt.Println(reflected)
}

Gives:

$ go run *go
&{0 }
<nil>

Isn't reflect.New supposed to return &{0 } too? (Runnable Version)

Ultimately, I wish to be able to iterate over the fields of the reflected struct (reflected.NumField() gives reflected.NumField undefined (type interface {} is interface with no methods)) and use SetInt, SetString and so on.

Thanks,





Aucun commentaire:

Enregistrer un commentaire