~~~
如 http.HandleFunc("/api/*",Test)
以上的*代表可以任意值 也能获取*的内容
~~~
更多评论
学习一下,目前没发现原生http.HandleFunc自定义路由的方法,gin是可以的。
1、gin
router:=gin.Default()
router.GET("/user/:name/*action", func(context *gin.Context) {
name := context.Param("name")
action := context.Param("action")
message := name + " is " + action
)}
router.Run(":3333")
请求示例:
curl -i http://localhost:3333/user/kebi/jumping
curl -i http://localhost:3333/user/xiaoming/running
2、是否可以特殊处理请求URL参数,例如如下demo
http.HandleFunc("/hello/", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var respStr = ""
//_, err2 := fmt.Fprintf(writer, "Hello %s!", request.URL.Path[len("/hello/"):])
urlPart := string(request.URL.Path[len("/hello/")])
switch urlPart {
case "1":
respStr = "1"
case "2":
respStr = "2"
default:
respStr = "不合法"
}
_, err := fmt.Fprintf(writer, "Hello "+respStr)
if err != nil {
log.Fatal(err)
}
}))
借贵宝地学习讨论一下,还请指教。
#1