在进行某种类型的验证之前,我必须检查 URL 参数是否存在。我该怎么做?
如果参数像这样变空:http://myurl.com?myparam=那么 myParam == ""是真的,但是如果 url 是这样来的 http://myurl.com (没有参数)然后 myParam == ""也是真的......所以我需要一些方法来验证参数是否出现在 url 中
# example
# http://myurl.com?myparam=johndoe
// validate if param exists, here i dont know how to do
#
#
// then do some validation
func validateMyParamIsNotNumber(r *http.Request, resultMessage *string) {
myParam := r.FormValue("myparam")
if myParam != "" && isNotNumber(product) {
*resultMessage = "The myparam filter must be a number"
return
}
}
最佳答案
检查 Request.Form 中是否存在 key 使用 map index with multiple assignment .在检查 map 之前解析表单。
func validateMyParamIsNotNumber(r *http.Request, resultMessage *string) {
r.ParseForm()
_, hasMyParam := r.Form["myparam"]
...
关于go - 如何检查 URL 参数是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57311158/