How to convert an HTML form value into an int in Golang

huangapple go评论162阅读模式
英文:

How to convert an HTML form value into an int in Golang

问题

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

func defineHandler(w http.ResponseWriter, r *http.Request) {
    a, err := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64)
    if err != nil {
        // 错误处理
    }
    b := r.FormValue("aRows")
    fmt.Fprintf(w, "aRows is: %s", b)
}

在编译过程中返回的错误是:"multiple-value strconv.ParseInt() in single-value context"。

我认为这与FormValue中的信息格式有关,但我不知道如何解决这个问题。

英文:

My test Handler code is here:

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);
}

The error returned during the compile comes out as:
"multiple-value strconv.ParseInt() in single-value context"

I believe it has to do with the format of information in the FormValue I just don't know how to alleviate that.

答案1

得分: 6

这意味着strconv.ParseInt有多个返回值(int和一个错误),所以你需要这样做:

a, err := strconv.ParseInt(r.FormValue("aRows")[0:], 10, 64);
if err != nil {
  // 以某种方式处理错误
}
英文:

It means that strconv.ParseInt has multiple return values (the int, and an error), so you need to do:

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

huangapple
  • 本文由 发表于 2013年10月8日 13:38:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/19239890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定