在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
                }
            }
        }

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
}

That's because v is a []string (slice of string), not a string (what Atoi accepts). Request.Form is a map of field names to values; because HTML forms allow for multiple fields by the same name, the values must be held in a slice. You can try v[0] to get the first value for the field, which may or may not do what you want - I can't say for sure since you don't detail what you're trying to accomplish.