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

use*_*946 3 html forms typeconverter go 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);
}
Run Code Online (Sandbox Code Playgroud)

编译期间返回的错误显示为:“单值上下文中的多值 strconv.ParseInt()”

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

Mat*_*rog 5

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

a, err := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64);
if err != nil {
  // handle the error in some way
}
Run Code Online (Sandbox Code Playgroud)