您可以使用標準庫或其他路由器來做到這壹點。我將介紹兩種方式:
標準庫:
import(
fmt
net//julienschmidt//julienschmidt//http
log
)
funcLocationHandler(whttp.ResponseWriter,r*http.Request,pshttprouter.Params){
fmt.Fprintf(w,Location:%s\n,ps.ByName(loc))
}
funcmain(){
router:=httprouter.New()
router.GET(/location/:loc,LocationHandler)
log.Fatal(http.ListenAndServe(:8080,router))
}
請註意,httprouter對處理程序使用稍微不同的簽名。這是因為,如您所見,它還將這些參數傳遞給函數。
哦,還有壹個註意事項,妳可以直接http://localhost:8080/location/titanrolex用妳的瀏覽器(或其他東西)點擊-如果其他東西足夠好,它會將URLEncode編碼為http://localhost:8080/location/titan%20rolex.