Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go fmt on a whole source tree

Tags:

go

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?

like image 885
Chris Avatar asked Nov 10 '12 04:11

Chris


People also ask

How do you run go FMT on all files?

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 .

What is go FMT command?

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.

How do I use Gofmt?

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.


1 Answers

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.

like image 51
tux21b Avatar answered Oct 24 '22 04:10

tux21b