如何在Golang中将HTML表单值转换为int

如何在Golang中将HTML表单值转换为int,html,go,forms,typeconverter,strconv,Html,Go,Forms,Typeconverter,Strconv,我的测试处理程序代码如下: func defineHandler(w http.ResponseWriter, r *http.Request) { a := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64); b := r.FormValue("aRows"); fmt.Fprintf(w, "aRows is: %s", b); } 编译期间返回的错误如下所示: “单值上下文中的多值strconv.ParseInt

我的测试处理程序代码如下:

func defineHandler(w http.ResponseWriter, r *http.Request) {
    a := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64);
    b := r.FormValue("aRows");
    fmt.Fprintf(w, "aRows is: %s", b);
}
编译期间返回的错误如下所示: “单值上下文中的多值strconv.ParseInt()”

我认为这与FormValue中的信息格式有关,我只是不知道如何缓解这种情况。

这意味着有多个返回值(int和一个错误),因此您需要执行以下操作:

a, err := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64);
if err != nil {
  // handle the error in some way
}
不那么尴尬。他们可以设计语言,使您的原始代码是正确的。许多语言中的
parseInt
都是这样的:如果出现错误,它会抛出异常或返回
NaN
或其他什么。Go的设计目的是促使您思考错误并加以处理。习惯需要一些时间。