Modernize Go map and slice operations#38126
Merged
Conversation
rastislavs
approved these changes
Mar 12, 2025
Member
Author
|
Incremental diff to address first round of reviews: diff --git a/pkg/debug/subsystem.go b/pkg/debug/subsystem.go
index 32402279db8f..9013cf1487d5 100644
--- a/pkg/debug/subsystem.go
+++ b/pkg/debug/subsystem.go
@@ -57,11 +57,9 @@ func (s *statusFunctions) registerStatusObject(name string, obj StatusObject) er
}
func (s *statusFunctions) collectStatus() StatusMap {
- fnCopy := functionMap{}
-
// Make a copy to not hold the mutex while collecting the status
s.mutex.RLock()
- maps.Copy(fnCopy, s.functions)
+ fnCopy := maps.Clone(s.functions)
s.mutex.RUnlock()
status := StatusMap{}
diff --git a/pkg/identity/cache/local.go b/pkg/identity/cache/local.go
index cc2aff6c7a88..70f05cedf719 100644
--- a/pkg/identity/cache/local.go
+++ b/pkg/identity/cache/local.go
@@ -238,14 +238,9 @@ func (l *localIdentityCache) lookupByID(id identity.NumericIdentity) *identity.I
// GetIdentities returns all local identities
func (l *localIdentityCache) GetIdentities() map[identity.NumericIdentity]*identity.Identity {
- cache := map[identity.NumericIdentity]*identity.Identity{}
-
l.mutex.RLock()
defer l.mutex.RUnlock()
-
- maps.Copy(cache, l.identitiesByID)
-
- return cache
+ return maps.Clone(l.identitiesByID)
}
func (l *localIdentityCache) checkpoint(dst []*identity.Identity) []*identity.Identity {
diff --git a/pkg/node/manager/rest_api.go b/pkg/node/manager/rest_api.go
index e8fa02508ed3..a37b805a3863 100644
--- a/pkg/node/manager/rest_api.go
+++ b/pkg/node/manager/rest_api.go
@@ -160,12 +160,9 @@ func (c *clusterNodesClient) NodeDelete(node nodeTypes.Node) error {
// If the node was added/updated and removed before the clusterNodesClient
// was aware of it then we can safely remove it from the list of added
// nodes and not set it in the list of removed nodes.
- found := -1
- for i, added := range c.NodesAdded {
- if added.Name == node.Fullname() {
- found = i
- }
- }
+ found := slices.IndexFunc(c.NodesAdded, func(added *models.NodeElement) bool {
+ return added.Name == node.Fullname()
+ })
if found != -1 {
c.NodesAdded = slices.Delete(c.NodesAdded, found, found+1)
} else { |
faa050b to
6ead8ac
Compare
Member
Author
Yes. I'll add |
Member
Author
Yes, see my comment above. I plan to add the linter to CI once all issues it reports are resolved. |
Member
Author
|
/test |
6ead8ac to
a620b81
Compare
Member
Author
|
/test |
rgo3
approved these changes
Mar 13, 2025
a620b81 to
db9c865
Compare
Member
Author
|
/test Rebased to resolve merge conflict |
Use the Copy and Clone functions from the maps standard library package,
added in Go 1.21.
Generated using modernize by running:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
and committing the relevant changes with some minor manual edits (e.g.
reordering added imports).
Signed-off-by: Tobias Klauser <[email protected]>
Use the convenience functions from the slices standard library package,
added in Go 1.21.
Generated using modernize by running:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
and committing the relevant changes with some minor manual edits (e.g.
reordering added imports).
Signed-off-by: Tobias Klauser <[email protected]>
db9c865 to
9981d34
Compare
Member
Author
|
/test ...and another rebase to resolve merge conflicts |
borkmann
approved these changes
Mar 14, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use the convenience functions from the
mapsandslicesstandard library packages, added in Go 1.21 and extended in the successive releases.Generated using modernize by running:
and committing the relevant changes with some minor manual edits (e.g. reordering added imports).
Sorry for the large PR touching many pieces of the code base. Best reviewed by filtering for "Only files owned by you" so you only see changes for your own review groups: