英文:
Trying to convert string to instance variable
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是GO语言的新手。
尝试通过构建真实的Web应用程序来学习GO。
我正在使用revel框架。
这是我的资源路由:
GET /resource/:resource Resource.ReadAll
GET /resource/:resource/:id Resource.Read
POST /resource/:resource Resource.Create
PUT /resource/:resource/:id Resource.Update
DELETE /resource/:resource/:id Resource.Delete
例如:
GET /resource/users
调用 Resource.ReadAll("users")
这是我的Resource控制器(目前只是一个虚拟操作):
type Resource struct {
*revel.Controller
}
type User struct {
Id int
Username string
Password string
}
type Users struct {}
func (u Users) All() string {
return "All"
}
func (c Resource) ReadAll(resource string) revel.Result {
fmt.Printf("GET %s", resource)
model := reflect.New(resource)
fmt.Println(model.All())
return nil
}
我试图通过将resource string转换为对象来获取Users结构体的实例,以调用All函数。
错误信息为:
无法将类型为string的resource用作reflect.Type的参数传递给reflect.New:string未实现reflect.Type(缺少Align方法)
我是GO的新手,请不要评判我
英文:
I'm new to GO language.
Trying to learn GO by building real web application.
I'm using revel framework.
And here is my resource routes:
GET /resource/:resource Resource.ReadAll
GET /resource/:resource/:id Resource.Read
POST /resource/:resource Resource.Create
PUT /resource/:resource/:id Resource.Update
DELETE /resource/:resource/:id Resource.Delete
for example:
GET /resource/users
calls Resource.ReadAll("users")
And this is my Resource controller (it's just a dummy actions for now):
type Resource struct {
*revel.Controller
}
type User struct {
Id int
Username string
Password string
}
type Users struct {}
func (u Users) All() string {
return "All"
}
func (c Resource) ReadAll(resource string) revel.Result {
fmt.Printf("GET %s", resource)
model := reflect.New(resource)
fmt.Println(model.All())
return nil
}
I'm trying get instance of Users struct by converting resource string to object to call All function.<br/>
and the error:
> cannot use resource (type string) as type reflect.Type in argument to
> reflect.New: string does not implement reflect.Type (missing Align
> method)
I'm new to GO please don't judge me
答案1
得分: 3
你的问题在这里:
model := reflect.New(resource)
你不能用这种方式从字符串实例化一个类型。你需要在那里使用switch,并根据模型执行相应的操作:
switch resource {
case "users":
model := &Users{}
fmt.Println(model.All())
case "posts":
// ...
}
或者正确使用reflect
。类似这样:
var types = map[string]reflect.Type{
"users": reflect.TypeOf(Users{}) // 或者 &Users{}.
}
// ...
model := reflect.New(types[resource])
res := model.MethodByName("All").Call(nil)
fmt.Println(res)
英文:
Your problem is here:
model := reflect.New(resource)
You can't instantiate a type from a string that way. You need to either use a switch there and do stuff depending on the model:
switch resource {
case "users":
model := &Users{}
fmt.Println(model.All())
case "posts":
// ...
}
Or use reflect
correctly. Something like:
var types = map[string]reflect.Type{
"users": reflect.TypeOf(Users{}) // Or &Users{}.
}
// ...
model := reflect.New(types[resource])
res := model.MethodByName("All").Call(nil)
fmt.Println(res)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论