I have code
var t reflect.Type = LaunchController(route.controller)
// create controller ptr .
var appControllerPtr reflect.Value = reflect.New(t)
fmt.Println(appControllerPtr) //#=> <**controller.AppController Value>
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? Also I cant understand why my reflect.New(t)
returns reflect.Value
with 2 asterisks in beginning **
You don't give your complete code, so I have to guess a bit, but I suspect that the Controller
field of the AppController
structure has a lower-case name. Right? Here is my attempt to produce a minimal example from your code: working (with upper-case field name) and non-working (with lower-case fieldname).
Also: where you write reflect.ValueOf(appController).Field(0)
, the appController
is already of type reflect.Value
, so the ValueOf
is not required. You can just write appController.Field(0)
as in the example code I linked above.