【发布时间】:2014-11-08 15:50:17
【问题描述】:
如何将当前用户的用户名或其他变量添加到 Go 中 URL 路径的末尾?
我尝试使用 http.Redirect(w, "/home/"+user, http.StatusFound),但这会造成无限重定向循环。
去:
func homeHandler(w http.ResponseWriter, r *http.Request) {
randIndex = rand.Intn(len(cityLibrary))
ImageDisplay()
WeatherDisplay()
dispdata = AllApiData{Images: imagesArray, Weather: &WeatherData{Temp: celsiusNum, City: cityLibrary[randIndex], RainShine: rainOrShine}}
//http.Redirect(w, "/"+cityLibrary[randIndex], http.StatusFound) --> infinite redirect loop
renderTemplate(w, "home", dispdata)
}
func main() {
http.HandleFunc("/", homeHandler)
http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(http.Dir("layout"))))
http.ListenAndServe(":8000", nil)
}
在这段代码中,我试图将“City”的值附加到根 URL 的末尾。我应该使用正则表达式吗?
【问题讨论】:
-
代码的其余部分是什么样的?路由器和处理程序?
-
添加了上面的代码。
-
/{city}/url 的处理程序在哪里? -
我没有。我不知道如何使用正则表达式构造它。
-
一秒钟我会写一个答案
标签: go