Я новичок в языке GO. Попытка узнать GO путем создания реального веб-приложения. Я использую revel framework.Пытается преобразовать строку в переменную экземпляра
А вот мои ресурсные маршруты:
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
}
Я пытаюсь получить экземпляр пользователей struct путем преобразования строка ресурса Объект для вызова Все функция.
и ошибка:
cannot use resource (type string) as type reflect.Type in argument to reflect.New: string does not implement reflect.Type (missing Align method)
Я новичок в GO, пожалуйста, не судите меня :)
Это то, что вы пытаетесь сделать? http://stackoverflow.com/questions/23030884/is-there-a-way-to-create-an-instance-of-a-struct-from-a-string – ANisus