gostatic-analysisgolangci-lint

Implicit memory aliasing in for loop


I'm using golangci-lint and I'm getting an error on the following code:

versions []ObjectDescription
... (populate versions) ...

for i, v := range versions {
    res := createWorkerFor(&v)
    ...

}

the error is:

G601: Implicit memory aliasing in for loop. (gosec)
                     res := createWorkerFor(&v)
                                            ^

What does "implicit memory aliasing in for loop" mean, exactly? I could not find any error description in the golangci-lint documentation. I don't understand this error.


Solution

  • Indexing will solve the problem:

    for i := range versions {
        res := createWorkerFor(&versions[i])
        ...
    
    }