Go 1.22 code simplification

347 views
Skip to first unread message

Amnon

unread,
Jan 16, 2024, 1:33:29 AM1/16/24
to golang-nuts
Go 1.22 contains some cool features which will allow us to write simpler code.

Would it be possible for gofmt -s to help us do transformations which clean up old code such as 
for i := 0; i < n; i++   -> for i := range n
removing x := x assignments inside range loops
or updating code to use the new math/rand/v2 API?

If not, has anyone written any refactoring tools to do this?


David Finkel

unread,
Jan 16, 2024, 1:44:59 PM1/16/24
to Amnon, golang-nuts
I don't know of anything that does those rewrites (and haven't looked), but it looks like gofmt -s could support it by extending this type-switch: https://cs.opensource.google/go/go/+/master:src/cmd/gofmt/simplify.go;l=16-103;drc=9b89c380208ea2e85985ee6bf2b1d684274dfa1d

I like the idea of an automatic rewrite of those loop primitives, though.

(it's possible that the x := x case may need type/declaration-location information that gofmt doesn't have to do robustly, though)

Note: the go fix directory hasn't had any commits since August, so it doesn't appear to have it either. (that might be the right place for that rewrite -- although, I think vet also has a suggested fix capacity that would make sense for any module with the go line set to 1.22+)

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/91a531a9-5043-4357-b643-d1600dcae119n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages