dimanche 7 juin 2015

Setting field using reflect.Set

I have code

var t reflect.Type = LaunchController(route.controller)

    // create controller ptr .
    var appControllerPtr reflect.Value = reflect.New(t)
    var appController reflect.Value = appControllerPtr.Elem()
    // Create and configure base controller
    var c *Controller = &Controller{
            Request: r,
            Writer: w,
            Name: t.Name(),
        }

    //this should assign *goninja.Controller field in application controllers
    var controllerField reflect.Value = reflect.ValueOf(appController).Field(0)
    controllerField.Elem().Set(reflect.ValueOf(c))

This creates pointer to element, and afterwards trying to assign value, into 0 field of this struct.

My struct, that i'm trying to reflect looks like

type AppController struct {
  *goninja.Controller
}

However when I'm trying to assign this field with code

controllerField.Elem().Set(reflect.ValueOf(c))

I'm facing following error

reflect: reflect.Value.Set using value obtained using unexported field

What am i doin wrong?





Aucun commentaire:

Enregistrer un commentaire