httprouter.go 602 B

12345678910111213141516171819202122232425262728
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/julienschmidt/httprouter"
  7. "github.com/sanity-io/litter"
  8. )
  9. func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  10. fmt.Fprint(w, "Welcome!\n")
  11. }
  12. func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  13. c := httprouter.ParamsFromContext(r.Context()) // nil ?
  14. fmt.Fprintln(w, litter.Sdump(c))
  15. fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
  16. }
  17. func main() {
  18. router := httprouter.New()
  19. router.GET("/", Index)
  20. router.GET("/hello/:name", Hello)
  21. log.Fatal(http.ListenAndServe(":8080", router))
  22. }