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

What is the purpose of "r := r"? #67

Open
utkusen opened this issue Nov 16, 2022 · 2 comments
Open

What is the purpose of "r := r"? #67

utkusen opened this issue Nov 16, 2022 · 2 comments

Comments

@utkusen
Copy link

utkusen commented Nov 16, 2022

Hi there,

I'm using your library on every project that I have. But there is a line in your example that I don't understand. I'm not an expert on Go, maybe it's something very basic. So forgive me if it's a silly question.

for _, r := range requests {
		r := r
		wp.Submit(func() {
			fmt.Println("Handling request:", r)
		})
	}

Why we are re-defining r there at r := r? We already have the value of r. The code doesn't work without that line. It became a mystery for me.

@wj-stack
Copy link

All anonymous functions created in the body of the loop capture the two variables themselves (the addresses of the variables), not the values of the two variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants