go语言中Pat多路复用器路由功能示例代码
Pat多路复用器是一个用于Go语言的net/http库的Sinatra风格的模式多路复用器。
安装pat包
go get github.com/bmizerany/pat
Pat多路复用示例代码:
package main
import (
"fmt"
"github.com/bmizerany/pat"
"net/http"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func ReplyNamePat(w http.ResponseWriter, r *http.Request) {
parameters := r.URL.Query()
name := parameters.Get(":name")
w.Write([]byte(fmt.Sprintf("Hello %s !", name)))
}
func main() {
mx := pat.New()
mx.Get("/", http.HandlerFunc(SayHelloWorld))
mx.Get("/:name", http.HandlerFunc(ReplyNamePat))
http.Handle("/", mx)
http.ListenAndServe(":8080", nil)
}
输出:
http://localhost:8080/zongscan
你好zongscan!
http://localhost:8080/
你好世界!
相关文章