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

redundant assignment statement for _,file := range files { file := file ...} #42141

Closed
XiaodongLoong opened this issue Oct 22, 2020 · 4 comments

Comments

@XiaodongLoong
Copy link
Contributor

There is a redundant assignment statement, I think it is no meaning:

file := file

@pierrec
Copy link

pierrec commented Oct 22, 2020

It is not redundant, it makes a copy of the file for the parallel test below.
See concurrency.

@martisch
Copy link
Contributor

martisch commented Oct 22, 2020

Note there is a long standing issue to detect the type of error that can happen if this copy was not made. #16520

I assume also the race detector may complain if the copy was not there.

@XiaodongLoong
Copy link
Contributor Author

Thanks, I have gained knowledge!

@XiaodongLoong
Copy link
Contributor Author

XiaodongLoong commented Oct 22, 2020

I write some code to test this issue:

package main

import "fmt"

func main() {
        a := []int{1,2,3}
        for x := range a {
                fmt.Println(x)
                fmt.Println(&x)
                x := x
                fmt.Println(&x)
        }
}

test logs:

$ go run main.go 
0
0xc00007e010
0xc00007e030
1
0xc00007e010
0xc00007e040
2
0xc00007e010
0xc00007e050

@golang golang locked and limited conversation to collaborators Oct 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants