Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: remove unnecessary redeclarations in for loops (part 2) #18593

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

Merged
merged 4 commits into from
Jun 26, 2025

Conversation

aslilac
Copy link
Member

@aslilac aslilac commented Jun 25, 2025

Now that we're post code-freeze, let's go forth with the other half of this cleanup.

Comment on lines -411 to -413
// When we assign the non-pointer to a
// variable it loses the reference.
replica := replica
Copy link
Member Author

@aslilac aslilac Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnstcn you called out this one as potentially being a concern, but I think there are already two "copies" happening here that make this safe

  • it gets copied from the slice to the replica variable by the loop
  • it gets copied from the replica variable to the argument value as part of the append call on the next line

ie. go does not have "references", it has pointers and values. you're either copying a pointer or you're copying the whole value.

here's a go playground that hopefully instills confidence https://go.dev/play/p/MfFltxQepYE

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aslilac!

@aslilac aslilac merged commit 09cc906 into main Jun 26, 2025
39 checks passed
@aslilac aslilac deleted the lilac/go-122-range-fix-all branch June 26, 2025 18:28
@github-actions github-actions bot locked and limited conversation to collaborators Jun 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants