我是GO語言的新手。 試圖通過構建真正的Web應用程序來學習GO。 我正在使用狂歡框架。嘗試將字符串轉換爲實例變量
這裏是我的資源路線:
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.ReadAll( 「用戶」)
這是我的資源控制器(它只是一個虛擬的動作):
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
}
我想獲取用戶實例通過轉換結構資源字符串來對象調用全部函數。
和錯誤:
cannot use resource (type string) as type reflect.Type in argument to reflect.New: string does not implement reflect.Type (missing Align method)
我是新來走請不要評判我:)
這是你正在嘗試做什麼? http://stackoverflow.com/questions/23030884/is-there-a-way-to-create-an-instance-of-a-struct-from-a-string – ANisus