Variable is being used but Golang lint complaining that the variable is not being used

1.1k Views Asked by At

Below is the code snippet where i have declared a for loop and getting index and box from the array of boxes. I am using the same index below in the if else block, but the golangci-lint is complaining about the variable index unused

func createMap(action string, boxes []Box, ratesMap FeeMap) []RateRes {

    resp := make([]RateRes, len(boxes))

    overrideKey := fmt.Sprintf("%s-*", action)

    for index, box := range boxes {
    
        key := fmt.Sprintf("%s-%s", action, boxes.label)
        rate, hasDefault := ratesMap[key]

        if hasDefault {
            resp[index] = RateRes{
                Code:  rate.Code,
                Label: box.label,
            }
        } else { 
            resp[index] = RateRes{
                Code:  RatesMap[overrideKey].Code,
                Label: box.label,
            }
        }

    }

    return resp
}

The exact error i am getting is : Error: index declared but not used (typecheck)

Please suggest how to overcome this issue, which does not seem an issue.

1

There are 1 best solutions below

1
On

In your for loop initialization statement you declare the box variable and it hasn't been used in the codes after that.

for index, box := range boxes {

please remove it if you are not using it change it like this.

for index := range boxes {