How To Run Gofmt Recursively

gofmt it’s a very useful tool coming directly with the go standard toolchain. Gofmt is a tool for formatting and linting your Go source code files. It uses tabs for indentation. Here’s how it works

gofmt -help
usage: gofmt [flags] [path ...]
-cpuprofile string
write cpu profile to this file
-d  display diffs instead of rewriting files
-e  report all errors (not just the first 10 on different lines)
-l  list files whose formatting differs from gofmt's
-r string
rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
-s  simplify code
-w  write result to (source) file instead of stdout

One common question when using gofmt is how to recursively run gofmt for your project, or how to recursively run gofmt for a certain director or file.

How to run gofmt recursively

gofmt -w -s .

This will run gofmt recursively and update the files directly

How to run gofmt recursively as dry-run

gofmt -s -d .

This will write the changes to standard output instead of writing directly to the files

Join the Golang Developers Community on Golang Cafe