Hi,
Been searching FAQ, documentation, the list archives and other websites for several days, and couldn't come up with a solution.
I'm trying to create a slice from a reflect.Type which I can pass to mgo for results retrieval. See the following function:
func GetList(w http.ResponseWriter, r *http.Request, m models.RegisteredModel) {
results := reflect.Zero(reflect.SliceOf(m.ModelStruct)).Interface()
fmt.Printf("%T %T", results, &results)
m.Collection.Find(bson.M{}).All(&results)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", Response{"items": results})
}
m.ModelStruct holds the reflect.Type of the needed struct. e.g, it's holds reflect.TypeOf of the following struct:
type Content struct {
Itype int `json:"itype", bson:"it"`
Title string `json:"title, "bson:"t"`
Metadata ItemMetadata `json:"metadata", bson:"md"`
Created time.Time `json:"created", bson:"c"`
Updated time.Time `json:"updated", bson:"u"`
}
The function above is wrapped with a closure which maps the for each registered model type. However, trying to access that handler yields the error:
[]models.Content *interface {}2013/12/01 11:24:44 http: panic serving [::1]:39634: result argument must be a slice address
Can one create a slice from a type determined at runtime (and use it with mgo, which excepts a slice) ?
Thanks