-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Modernize Go map and slice operations #38126
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
Conversation
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
Yes. I'll add |
Yes, see my comment above. I plan to add the linter to CI once all issues it reports are resolved. |
/test |
6ead8ac
to
a620b81
Compare
/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neaaat! 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hubble changes LGTM, thanks @tklauser 🎉
a620b81
to
db9c865
Compare
/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 <tobias@cilium.io>
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 <tobias@cilium.io>
db9c865
to
9981d34
Compare
/test ...and another rebase to resolve merge conflicts |
Use the convenience functions from the
maps
andslices
standard 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: