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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题