古詩詞大全網 - 個性簽名 - golang從postman傳入的數據在方法中如何接受

golang從postman傳入的數據在方法中如何接受

正在做的是綁定要處理的完整路徑。/location/{titanrolex}GetUser。您真正想要的是綁定/location/以由壹個處理程序處理(例如LocationHandler)。

您可以使用標準庫或其他路由器來做到這壹點。我將介紹兩種方式:

標準庫:

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.