3 回答

TA貢獻1719條經驗 獲得超6個贊
沒有內置的簡單方法可以做到這一點,但是,這并不難。
我就是這樣做的,沒有添加特定的庫。它被放置在一個函數中,以便您可以getCode()在請求處理程序中調用一個簡單的函數。
基本上你只是把它們r.URL.Path分成幾部分,然后分析這些部分。
// Extract a code from a URL. Return the default code if code
// is missing or code is not a valid number.
func getCode(r *http.Request, defaultCode int) (int, string) {
p := strings.Split(r.URL.Path, "/")
if len(p) == 1 {
return defaultCode, p[0]
} else if len(p) > 1 {
code, err := strconv.Atoi(p[0])
if err == nil {
return code, p[1]
} else {
return defaultCode, p[1]
}
} else {
return defaultCode, ""
}
}
- 3 回答
- 0 關注
- 516 瀏覽
添加回答
舉報