24. GoWebの簡単な使い方
func main() {
handler := func(c *goweb.Context) {
name := c.PathParams["name"]
animal := c.PathParams["animal"]
fmt.Fprintf(c.ResponseWriter, "Hey %s,
your favorite animal is a %s", name, animal)
}
goweb.MapFunc("/people/{name}/animals/
{animal}", handler)
goweb.ListenAndServe(":8080")
}
https://github.com/golang-samples/goweb/tree/master/mapfunc
25. GoWebの簡単な使い方 RESTful API
// REST用のコントローラー
type MyController struct {}
HTTPメソッド URI例 実装するメソッド
POST /api/ RestCreater.Create
GET /api/ RestManyReader.ReadMany
GET /api/123 RestReader.Read
PUT /api/ RestManyUpdater.UpdateMany
PUT /api/123 RestUpdater.Update
DELETE /api/ RestManyDeleter.DeleteMany
DELETE /api/123 RestDeleter.Delete
※必要なものだけ、実装する
26. GoWebの簡単な使い方 RESTful API
// POSTをハンドリングする
func (cr *MyController) Create(cx
*goweb.Context) {
fmt.Fprintf(cx.ResponseWriter,
"Create new resource")
}