为什么我的Go代码没有运行?

时间:2013-03-28 11:15:54

标签: go

package main

import (
    "fmt"
    "github.com/ant0ine/go-json-rest"
    "net/http"
)

type App struct {
    Id   string
    Name string
}

func GetApp(w *rest.ResponseWriter, req *rest.Request) {
    user := App{
        Id:   req.PathParam("id"),
        Name: "Antoine",
    }
    w.WriteJson(&user)
}

type MyResourceHandler struct {
    rest.ResourceHandler
}

type ResourceController interface {
    Show(w *rest.ResponseWriter, req *rest.Request)
    Create(w *rest.ResponseWriter, req *rest.Request)
    Update(w *rest.ResponseWriter, req *rest.Request)
    Delete(w *rest.ResponseWriter, req *rest.Request)
}

func (self *MyResourceHandler) AddResource(name string, c ResourceController) error {
    show_func := func(w *rest.ResponseWriter, r *rest.Request) {
        c.Show(w, r)
    }
    create_func := func(w *rest.ResponseWriter, r *rest.Request) {
        c.Create(w, r)
    }
    update_func := func(w *rest.ResponseWriter, r *rest.Request) {
        c.Update(w, r)
    }
    delete_func := func(w *rest.ResponseWriter, r *rest.Request) {
        c.Delete(w, r)
    }

    err := self.ResourceHandler.SetRoutes(
        rest.Route{"GET", fmt.Sprintf("/%s/:id", name), show_func},
        rest.Route{"POST", fmt.Sprintf("/%s", name), create_func},
        rest.Route{"PUT", fmt.Sprintf("/%s/:id", name), update_func},
        rest.Route{"DELETE", fmt.Sprintf("/%s/:id", name), delete_func},
    )
    return err
}

type AppController struct{}

func (self *AppController) Show(w *rest.ResponseWriter, r *rest.Request) {
    app := App{
        Id:   r.PathParam("id"),
        Name: "Antoine",
    }
    w.WriteJson(&app)
}

func (self *AppController) Create(w *rest.ResponseWriter, r *rest.Request) {
    app := App{
        Id:   r.PathParam("id"),
        Name: "Antoine",
    }
    w.WriteJson(&app)
}

func (self *AppController) Update(w *rest.ResponseWriter, r *rest.Request) {
    app := App{
        Id:   r.PathParam("id"),
        Name: "Antoine",
    }
    w.WriteJson(&app)
}

func (self *AppController) Delete(w *rest.ResponseWriter, r *rest.Request) {
    app := App{
        Id:   r.PathParam("id"),
        Name: "Antoine",
    }
    w.WriteJson(&app)
}

func main() {
    handler := MyResourceHandler{}
    controler := AppController{}
    handler.AddResource("app", controler)
    http.ListenAndServe(":9008", &handler)
}

我已经实现了接口ResourceController的所有四种方法,但它告诉我:

./fakeapi.go:93: cannot use controler (type AppController) as type ResourceController in function argument:
    AppController does not implement ResourceController (Create method requires pointer receiver)

3 个答案:

答案 0 :(得分:4)

它出现在错误消息中:(Create method requires pointer receiver)

您已在*AppController(指向AppController的指针)上定义了创建,但您尝试使用不满足该接口的AppController

您可能想要的解决方案是将controler := AppController{}更改为controler := &AppController{}

答案 1 :(得分:2)

正如cthom06所说,答案是错误的。

出现错误消息的原因是,当您传递指针而不是值时,您只实现了ResourceController

当您实施ShowCreate等时,您使用了

func (self *AppController)...

这需要方法来获取指针接收器,但AddResources被赋予controler,而不是指针。

以下是一些显示问题的示例代码:

package main

import "fmt"

type I interface {
    Show(i int)
}

type T struct{}

func (t *T) Show(i int) {
    fmt.Println(i)
}

func CallShow(i I) {
    i.Show(32);
}

func main() {
    obj := T{}

    // CallShow(obj) // This doesn't work unless you change func (t *T) to func (t T)
    CallShow(&obj) // This works because you pass a pointer
}

答案 2 :(得分:0)

也弃用“ResourceHandler”。我建议使用v3 API:

api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
   // your routes here ...
)
if err != nil {
    log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))

Antoine - go-json-rest作者