Dear all,
I am a Python developer and I am just playing with golang. I tried a simple REST server but I am having some problem in compile time, but I don’t know why. The code snippet is show below.
AFAIK It follows the indications posted on https://github.com/go-chi/chi/blob/master/middleware/url_format.go
The render.JSON signature is JSON(w http.ResponseWriter, r *http.Request, v interface{})
It must be and absurd error but I am new into golang. Any one can spot and explain where the error is ?
Thanks for your time!
Xavi
package main
import (
"net/http"
"github.com/gin-gonic/gin/render"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
type articles struct {
Name string `json:"name" xml:"name"`
}
func routes() http.Handler {
r := chi.NewRouter()
r.Use(middleware.URLFormat)
r.Get("/articles/{id}", listArticles)
return r
}
func listArticles(w http.ResponseWriter, r *http.Request) {
urlFormat, _ := r.Context().Value(middleware.URLFormatCtxKey).(string)
switch urlFormat {
case "json":
render.JSON(w, r, articles{"json"})
case "xml":
render.XML(w, r, articles{"xml"})
default:
render.JSON(w, r, articles{"default"})
}
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Mount("/", routes())
http.ListenAndServe(":3333", r)
}