Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: complains about captured range var that's actually a field name #13236

Closed
Sajmani opened this issue Nov 13, 2015 · 1 comment
Closed

Comments

@Sajmani
Copy link
Contributor

Sajmani commented Nov 13, 2015

go version devel +4df6dcb Wed Jul 22 16:12:06 2015 -0400 linux/amd64

Vet complains that "v" is captured by this loop; it isn't:

package main

import "fmt"

func main() {
    list := []int{1, 2, 3}
    type s struct {
        v int
    }
    c := make(chan s)
    for v := range list {
        go func(vv int) {
            c <- s{v: vv} // vet complains that v is captured here
        }(v)
    }
    for range list {
        fmt.Println(<-c)
    }
}

Output:

% go run main.go      
{2}
{0}
{1}
% go tool vet main.go
main.go:13: range variable v captured by func literal
@ianlancetaylor ianlancetaylor changed the title x/tools/cmd/vet: complains about captured range var that's actually a field name cmd/vet: complains about captured range var that's actually a field name Nov 14, 2015
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Nov 14, 2015
@gopherbot
Copy link

CL https://golang.org/cl/21183 mentions this issue.

@golang golang locked and limited conversation to collaborators Mar 27, 2017
@rsc rsc unassigned adg Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants