duanchen1937 2018-01-04 19:51 采纳率: 0%
浏览 109
已采纳

在for循环中使用AtoI时出错

I am new to the go lang and am sure I am missing something very basic but since I have scoured the web and have not found anything to help me understand my problem I thought I would post it here. Essentially what I am trying to do is a simple conversion from a string to an int. The string arrives as a numerical value represented as a string (ie. "1"); I would like to change it to the int equivalent so I can use it in a switch ... case. I have tried both Atoi and parseInt and both fail with the same error:

./test.go:1765: cannot use v (type []string) as type string in argument to strconv.Atoi

I tried to assign it to a string first and then pass it in to Atoi but it still didn't work.

Here is the code:

        r.ParseForm()

        for k, v := range r.Form {
            if k == "StartPicker" && strings.Join(v, "") != "" {
               layout := dateLayout
               startDate, err = time.Parse(layout, strings.Join(v, ""))

               if err != nil {
                   fmt.Println("time.Parse() err:", err)
                }

            } else if k == "EndPicker" && strings.Join(v, "") != "" {
                layout := dateLayout
                endDate, err = time.Parse(layout, strings.Join(v, ""))

                if err != nil {
                    fmt.Println("time.Parse() err:", err)
                }
            } else {
                fmt.Printf("k = %s
", k)

                if k == "FilterType" {
                    fmt.Printf("this is the variable filterType: %s
", v)
                    time, sqlFilter := strconv.Atoi(v)
                    if err != nil {
                        // handle error
                        fmt.Println("Atoi broken err:", err)
                    }

                } else if k == "MSISDN" {
                    msisdn := v
                }
            }
        }
  • 写回答

2条回答 默认 最新

  • douzhenqun1271 2018-01-04 20:23
    关注

    The error is telling you that you are passing a []string to a string argument.

    If the application is not working with multiple values for a form field and the application does not need to make a distinction between a missing form field and "", then it's typical to call Request.FormValue to get form values. This method returns the first value for the key or "" if the key is not present.

    There's no need to loop over form fields. Just fetch the fields you want.

    Here's how to write the code above.

    var startDate time.Time
    if v := r.FormValue("StartPicker"); v != "" {
        var err error
        startDate, err = time.Parse(dateLayout, v)
        if err != nil {
            // handle error
        }
    }
    
    var endDate time.Time
    if v := r.FormValue("EndPicker"); v != "" {
        var err error
        startDate, err = time.Parse(dateLayout, v)
        if err != nil {
            // handle error
        }
    }
    
    var filterType int
    if v := r.FormValue("FilterType"); v != "" {
        var err error
        filterType, err = strconv.Atoi(v)
        if err != nil {
            // handle error
        }
    }
    
    msisdn := r.FormValue("MSISDN")
    

    If the client is expected to specify a field, then eliminate the if v := r.FormValue("key"); v != nil business.

    filterType, err = strconv.Atoi(r.FormValue("FilterType"))
    if err != nil {
        // handle error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值