尝试将字符串转换为实例变量。

huangapple go评论113阅读模式
英文:

Trying to convert string to instance variable

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是GO语言的新手。
尝试通过构建真实的Web应用程序来学习GO。
我正在使用revel框架。

这是我的资源路由:

  1. GET /resource/:resource Resource.ReadAll
  2. GET /resource/:resource/:id Resource.Read
  3. POST /resource/:resource Resource.Create
  4. PUT /resource/:resource/:id Resource.Update
  5. DELETE /resource/:resource/:id Resource.Delete

例如:

GET /resource/users 调用 Resource.ReadAll("users")

这是我的Resource控制器(目前只是一个虚拟操作):

  1. type Resource struct {
  2. *revel.Controller
  3. }
  4. type User struct {
  5. Id int
  6. Username string
  7. Password string
  8. }
  9. type Users struct {}
  10. func (u Users) All() string {
  11. return "All"
  12. }
  13. func (c Resource) ReadAll(resource string) revel.Result {
  14. fmt.Printf("GET %s", resource)
  15. model := reflect.New(resource)
  16. fmt.Println(model.All())
  17. return nil
  18. }

我试图通过将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:

  1. GET /resource/:resource Resource.ReadAll
  2. GET /resource/:resource/:id Resource.Read
  3. POST /resource/:resource Resource.Create
  4. PUT /resource/:resource/:id Resource.Update
  5. 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):

  1. type Resource struct {
  2. *revel.Controller
  3. }
  4. type User struct {
  5. Id int
  6. Username string
  7. Password string
  8. }
  9. type Users struct {}
  10. func (u Users) All() string {
  11. return "All"
  12. }
  13. func (c Resource) ReadAll(resource string) revel.Result {
  14. fmt.Printf("GET %s", resource)
  15. model := reflect.New(resource)
  16. fmt.Println(model.All())
  17. return nil
  18. }

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

你的问题在这里:

  1. model := reflect.New(resource)

你不能用这种方式从字符串实例化一个类型。你需要在那里使用switch,并根据模型执行相应的操作:

  1. switch resource {
  2. case "users":
  3. model := &Users{}
  4. fmt.Println(model.All())
  5. case "posts":
  6. // ...
  7. }

或者正确使用reflect。类似这样:

  1. var types = map[string]reflect.Type{
  2. "users": reflect.TypeOf(Users{}) // 或者 &Users{}.
  3. }
  4. // ...
  5. model := reflect.New(types[resource])
  6. res := model.MethodByName("All").Call(nil)
  7. fmt.Println(res)
英文:

Your problem is here:

  1. 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:

  1. switch resource {
  2. case &quot;users&quot;:
  3. model := &amp;Users{}
  4. fmt.Println(model.All())
  5. case &quot;posts&quot;:
  6. // ...
  7. }

Or use reflect correctly. Something like:

  1. var types = map[string]reflect.Type{
  2. &quot;users&quot;: reflect.TypeOf(Users{}) // Or &amp;Users{}.
  3. }
  4. // ...
  5. model := reflect.New(types[resource])
  6. res := model.MethodByName(&quot;All&quot;).Call(nil)
  7. fmt.Println(res)

huangapple
  • 本文由 发表于 2015年9月7日 18:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/32436827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定