我是golang的新手,并使用julienschmidt / httprouter进行路由。 基于以下片段,能够发送一个参数。 但我很困惑发送多个参数,云有人帮助我。
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
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))
}
答案 0 :(得分:1)
只需添加另一个参数:
router.GET("/hello/:first_name/:last_name", Hello)
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s %s!\n", ps.ByName("first_name"), ps.ByName("last_name"))
}