1
我正在使用大猩猩mux獲取模式值。如何處理一個空的變量,像這樣:前往:處理大猩猩mux空變量
轉到:
func ProductHandler (w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
a := vars["key"]
if a = "" { //does not seem to register empty string
//do something
} else
//do something
}
var r = mux.NewRouter()
func main() {
r.HandleFunc("/products/{key}", ProductHandler)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
當我鍵入URL www.example.com/products或www.example.com/products/我得到一個404頁沒有發現錯誤。我如何處理ProductHandler中的空變量?
http://www.gorillatoolkit.org/pkg/mux
可能重複[如何創建帶有可選的URL VAR路線使用大猩猩mux?](http://stackoverflow.com/questions/18503189/how-to-create-a-route-with-optional-url-var-using-gorilla-mux) – 2014-10-03 15:33:47
@SvenGrosen你不應該用手寫這個。當您因重複而投了票時,它會自動爲您發佈:P – thwd 2014-10-03 15:37:05
@tomwilde我沒有手寫它,只是標記了它。 – 2014-10-03 15:39:03