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

Fix test TestServer #27

Closed
wants to merge 1 commit into from

Conversation

tc-hib
Copy link

@tc-hib tc-hib commented Dec 5, 2020

  • typo in comments
  • loop variable captured in function literal

See https://golang.org/doc/faq#closures_and_goroutines

* typo in comments
* loop variable captured in function literal

See https://golang.org/doc/faq#closures_and_goroutines
@tc-hib
Copy link
Author

tc-hib commented Dec 5, 2020

See this example: playground
You can run go vet too, and see the warning.
You might want to have look at this issue too.

In a nutshell, if you capture a loop variable in an inner goroutine, you can't predict what value it will have on actual execution. Most of the time you'll only get a few samples repeated many times.

@alexandrevicenzi
Copy link
Owner

Yeah, but you do not fix the issue by removing the code, you should pass name to the goroutine to fix it properly, the same way as done by id.

@tc-hib
Copy link
Author

tc-hib commented Dec 7, 2020

I'm sorry I don't understand.
What code did I remove?
I only added a shadow variable.

@alexandrevicenzi
Copy link
Owner

I'm probably missing coffee, I thought you removed the code, but you added. Now things do make sense.

Yet, if you pass the variable to the goroutine, it looks cleaner than redefining it. Perhaps you can change it.

@tc-hib
Copy link
Author

tc-hib commented Dec 7, 2020

image

@alexandrevicenzi
Copy link
Owner

I'm not looking for easier, I'm looking for a cleaner solution. What someone would put an eye on and get right at it, redefining it is something very Go specific, that from Python, Java or other language perspective makes zero sense.

@tc-hib
Copy link
Author

tc-hib commented Dec 7, 2020

Do as you wish, it's your project.
But it seems to be the idiomatic Go way of solving this issue, easier to read and maintain (no parameter order).
This how Goland solves it automatically too.
It's already a closure anyway, so why make it more complicated?

@alexandrevicenzi
Copy link
Owner

id is already a parameter, leaving name outside is not concise. Two ways of doing the same thing in the same scope.

@tc-hib tc-hib closed this Dec 14, 2020
@tc-hib tc-hib deleted the PR_FixTestServer branch December 14, 2020 17:01
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

Successfully merging this pull request may close these issues.

2 participants