Friday humour: gofmt -r ❤

Here’s some friday humour for you all… :wink:

I love using gofmt for safe-ish renames.

Today I was staring at some code that I wrote a while back that had variables p and rn passed around everywhere. After figuring out what the hell those actually were (the absolute path to a file and the relative name of it) I decided to fix these names once and for all. Gofmt should handle that better than my editor as just p can be tricky to search and replace on.

$ gofmt -w -l -r 'p -> absPath' walk.go

Hmm. That’s a large diff…

:smiley:

9 Likes

What…

Needs moar absPath

2 Likes

gofmt's rewrite expressions take a single letter to be a place holder matching any identifier, which is why you got the result you did.

In general, https://godoc.org/golang.org/x/tools/cmd/gorename is better for these kind of transformations.

I have pushed a few commits recently when I changed protobuf libraries across a number of repositories. Having the gofmt command in the commit made it clear.

2 Likes

gofmt’s rewrite expressions take a single letter to be a place holder matching any identifier

I’ve been thrown off by single letter wildcards in gofmt more than once. Isn’t it counterintuitive? Go encourages short and single letter name for variables but when you want to operate on them with gofmt, you need to make an extra effort to not hodor hodor hodor.

Hodor?

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.