Skip to content

Use modern Go constructs, part 2#38652

Merged
tklauser merged 6 commits intomainfrom
pr/tklauser/modernize-all
Apr 3, 2025
Merged

Use modern Go constructs, part 2#38652
tklauser merged 6 commits intomainfrom
pr/tklauser/modernize-all

Conversation

@tklauser
Copy link
Copy Markdown
Member

@tklauser tklauser commented Apr 1, 2025

Follow-up to #38126 and #38157. Change all the remaining instances where modern Go constructs may be used.

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).

The only change that was left out was replacing omitempty by omitzero in structs since these might introduce API-breaking changes and need to be reviewed individually. This will be done in a separate follow-up PR together with enabling modernize in CI.

See individual commit messages for details.


Sorry for the huge PR touching many pieces of the code base and requiring many code reviewers 😢 Best reviewed by filtering for "Only files owned by you" so you only see changes for your own review groups:

image

@tklauser tklauser added the release-note/misc This PR makes changes that have no direct user impact. label Apr 1, 2025
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 1, 2025

/test

@github-actions github-actions bot added sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. hubble-cli PRs or GitHub issues related with hubble-cli cilium-cli This PR contains changes related with cilium-cli labels Apr 1, 2025
@tklauser tklauser changed the title Use modern Go constructs, take 2 Use modern Go constructs, part 2 Apr 1, 2025
@tklauser tklauser force-pushed the pr/tklauser/modernize-all branch from ed499c7 to 12cb4b8 Compare April 1, 2025 12:27
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 1, 2025

/test

@tklauser tklauser marked this pull request as ready for review April 1, 2025 12:59
@tklauser tklauser requested review from a team as code owners April 1, 2025 12:59
Copy link
Copy Markdown
Member

@giorio94 giorio94 left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Copy Markdown
Member

@nathanjsweet nathanjsweet left a comment

Choose a reason for hiding this comment

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

Very modern.

@tklauser tklauser force-pushed the pr/tklauser/modernize-all branch from a43d9f4 to 0d563f2 Compare April 3, 2025 07:16
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 3, 2025

/test

@tklauser tklauser force-pushed the pr/tklauser/modernize-all branch from 0d563f2 to f2121a3 Compare April 3, 2025 12:59
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 3, 2025

/test

Rebased to resolve merge conflict

@tklauser tklauser removed the request for review from liyihuang April 3, 2025 13:01
@tklauser tklauser force-pushed the pr/tklauser/modernize-all branch from f2121a3 to 94d0bbd Compare April 3, 2025 13:50
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 3, 2025

/test

Yet another rebase to resolve merge conflicts 😅

@tklauser tklauser added this pull request to the merge queue Apr 3, 2025
@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Apr 3, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Apr 3, 2025
tklauser added 6 commits April 3, 2025 17:36
Replace any 3-clause for i := 0; i < n; i++ {} loop by for i := range n
{}, added in Go 1.22.

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.

Signed-off-by: Tobias Klauser <[email protected]>
Replace interface{} by the 'any' type added in Go 1.18.

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.

Signed-off-by: Tobias Klauser <[email protected]>
Replace uses of context.WithCancel in tests with testing.T.Context,
added in Go 1.24.

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.

Signed-off-by: Tobias Klauser <[email protected]>
Replace []byte(fmt.Sprintf(...)) by fmt.Appendf(nil, ...), added in
Go 1.19.

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.

Signed-off-by: Tobias Klauser <[email protected]>
Use testing.B.Loop instead of a manual `for range b.N` loop. This also
removes the need to use testing.B.ResetTimer. See
golang/go#61515 for details.

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.

Signed-off-by: Tobias Klauser <[email protected]>
Replace if/else conditional assignments by a call to the
built-in min or max functions 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.

Signed-off-by: Tobias Klauser <[email protected]>
@tklauser tklauser force-pushed the pr/tklauser/modernize-all branch from 94d0bbd to 7d2005c Compare April 3, 2025 15:37
@tklauser
Copy link
Copy Markdown
Member Author

tklauser commented Apr 3, 2025

/test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cilium-cli This PR contains changes related with cilium-cli hubble-cli PRs or GitHub issues related with hubble-cli ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.