httprouter

https://github.com/julienschmidt/httprouter

package main

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
	router := httprouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	log.Fatal(http.ListenAndServe(":8080", router))
}
route实现ServerHTTP方法
路由存储在trees中,
Priority   Path             Handle
9          \                *
3          ├s               nil
2          |├earch\         *
1          |└upport\        *
2          ├blog\           *
1          |    └:post      nil
1          |         └\     *
2          ├about-us\       *
1          |        └team\  *
1          └contact\        *

https://github.com/julienschmidt/httprouter
route存储示例:
package main

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s-%s!\n", ps.ByName("name"), ps.ByName("age"))
}

func main() {
	router := httprouter.New()
	router.GET("/aa/bb/cc", Index)
	router.GET("/aa/mm/nn", Index)
	router.GET("/dd/ee", Index)
	router.GET("/fff/gg/*test", Index)
	router.GET("/hello/:name/:age", Hello)
	router.GET("/xx/yy/:name/:age", Hello)
	router.Print()

	log.Fatal(http.ListenAndServe(":8080", router))
}

trees存储为:
root:
&{%!t(string=/) false %!t(httprouter.nodeType=1) %!t(uint8=2) %!t(string=adfhx) [%!t(*httprouter.node=&{aa/ false 0 0 bm [0xc8200145f0 0xc820014640]  2}) %!t(*httprouter.node=&{dd/ee false 0 0  [] 0x2040 1}) %!t(*httprouter.node=&{fff/gg false 0 1 / [0xc820014780]  1}) %!t(*httprouter.node=&{hello/ true 0 2  [0xc820014870]  1}) %!t(*httprouter.node=&{xx/yy/ true 0 2  [0xc8200149b0]  1})] %!t(httprouter.Handle=) %!t(uint32=6)}
children:
	&{%!t(string=aa/) false %!t(httprouter.nodeType=0) %!t(uint8=0) %!t(string=bm) [%!t(*httprouter.node=&{bb/cc false 0 0  [] 0x2040 1}) %!t(*httprouter.node=&{mm/nn false 0 0  [] 0x2040 1})] %!t(httprouter.Handle=) %!t(uint32=2)}
	&{%!t(string=dd/ee) false %!t(httprouter.nodeType=0) %!t(uint8=0) %!t(string=) [] %!t(httprouter.Handle=0x2040) %!t(uint32=1)}
	&{%!t(string=fff/gg) false %!t(httprouter.nodeType=0) %!t(uint8=1) %!t(string=/) [%!t(*httprouter.node=&{ true 3 1  [0xc8200147d0]  1})] %!t(httprouter.Handle=) %!t(uint32=1)}
	&{%!t(string=hello/) true %!t(httprouter.nodeType=0) %!t(uint8=2) %!t(string=) [%!t(*httprouter.node=&{:name false 2 2  [0xc8200148c0]  1})] %!t(httprouter.Handle=) %!t(uint32=1)}
	&{%!t(string=xx/yy/) true %!t(httprouter.nodeType=0) %!t(uint8=2) %!t(string=) [%!t(*httprouter.node=&{:name false 2 2  [0xc820014a00]  1})] %!t(httprouter.Handle=) %!t(uint32=1)}

关于 liuzhantao

北京互联网求职群:168047123
此条目发表在 httprouter 分类目录。将固定链接加入收藏夹。