I have a project currently organized something like this:
~/code/go /bin /pkg /src /proj/main.go /some_package/package.go /some_other_package/some_other_package.go
Now if I want to use the go fmt
tool on my whole project it seems that the only way is to do it separately for each directory in my projects source tree:
go fmt proj go fmt proj/package go fmt proj/some_other_package
Is there some way to tell the fmt command to run on the whole source tree?
To run go formatter recursively on all project's files simply use: gofmt -s -w . To print the files that has been changed add -l option: gofmt -l -s -w .
Gofmt is a tool that automatically formats Go source code. Gofmt'd code is: easier to write: never worry about minor formatting concerns while hacking away, easier to read: when all code looks the same you need not mentally convert others' formatting style into something you can understand.
gofmt read the go program and show the result after indentation, vertical alignment and even re formats comments too. gofmt filename : This will print reformatted code. gofmt -w filename : This will reformat the code and updates the file.
You can use three dots (...
) as a wildcard. So for example, the following command will format all github.com packages:
go fmt github.com/...
This wildcard also works with other go commands like go list
, go get
and so. There is no need to remember such an ugly find command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With