go>> pat>> 返回
项目作者: gorilla

项目描述 :
a pretty simple HTTP router for Go.
高级语言: Go
项目地址: git://github.com/gorilla/pat.git
创建时间: 2012-10-02T21:35:01Z
项目社区:https://github.com/gorilla/pat

开源协议:BSD 3-Clause "New" or "Revised" License

下载


pat

testing
codecov
godoc
sourcegraph

Gorilla Logo

Install

With a properly configured Go toolchain:

  1. go get github.com/gorilla/pat

Example

Here’s an example of a RESTful api:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/gorilla/pat"
  6. )
  7. func homeHandler(wr http.ResponseWriter, req *http.Request) {
  8. wr.WriteHeader(http.StatusOK)
  9. wr.Write([]byte("Yay! We're home, Jim!"))
  10. }
  11. func getAllTheThings(wr http.ResponseWriter, req *http.Request) {
  12. wr.WriteHeader(http.StatusOK)
  13. wr.Write([]byte("Look, Jim! Get all the things!"))
  14. }
  15. func putOneThing(wr http.ResponseWriter, req *http.Request) {
  16. wr.WriteHeader(http.StatusOK)
  17. wr.Write([]byte("Look, Jim! Put one thing!"))
  18. }
  19. func deleteOneThing(wr http.ResponseWriter, req *http.Request) {
  20. wr.WriteHeader(http.StatusOK)
  21. wr.Write([]byte("Look, Jim! Delete one thing!"))
  22. }
  23. func main() {
  24. router := pat.New()
  25. router.Get("/things", getAllTheThings)
  26. router.Put("/things/{id}", putOneThing)
  27. router.Delete("/things/{id}", deleteOneThing)
  28. router.Get("/", homeHandler)
  29. http.Handle("/", router)
  30. log.Print("Listening on 127.0.0.1:8000...")
  31. log.Fatal(http.ListenAndServe(":8000", nil))
  32. }

Notice how the routes descend? That’s because Pat will take the first route
that matches.
For your own testing, take the line router.Get("/", homeHandler) and put it above the other routes and run the example. When you
try to curl any of the routes, you’ll only get what the homeHandler returns.
Design your routes carefully.