2013-03-28 100 views
-2
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的四種方法,但它告訴我:爲什麼我的Go代碼不能運行?

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

回答

4

這是錯誤消息:(Create method requires pointer receiver)

您已經定義創建於*AppController(指針到一個AppController),但您試圖使用AppController,這不符合接口。

你可能想解決的辦法是改變controler := AppController{}controler := &AppController{}

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 
} 
0

而且 「ResourceHandler」 已經過時了。我會建議使用第3版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())) 

安託萬 - 去 - JSON的REST作者

相關問題