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

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

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 &quot;users&quot;:
    model := &amp;Users{}
    fmt.Println(model.All())
case &quot;posts&quot;:
    // ...
}

Or use reflect correctly. Something like:

var types = map[string]reflect.Type{
    &quot;users&quot;: reflect.TypeOf(Users{}) // Or &amp;Users{}.
}

// ...

model := reflect.New(types[resource])
res := model.MethodByName(&quot;All&quot;).Call(nil)
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:

确定