Thursday, July 9, 2015

Formatting Go Code with Gofmt Tool

My Code Format/Style is Better than Yours

How many times have you experienced a cold war like situation in your team because each one of you have your own way to format code when it comes to indentation, spacing and brace positioning?

Go has Only One official Style & that is Enforced

Go designers have taken care of this and have come up with a tool named Gofmt that can format Go code in the official way :) Now there's no more:

  • debate over spacing, indentation and brace positions.
  • worries about minor formatting while writing code.
  • worries about converting someone's else's formatting style to your own to make the code more readable.

Here's a sample of a code that is working but is not formatted well.


shabbily formatted Go code. Also notice extra braces in Printf


Above go code file named gofmt.go is a working code where the developer has not taken care of the spacing, see { in line#3; has placed unnecessary braces, see line#4. Also there's no white-space between package, import and func, see line# 1, 2 and 3. 

How can we format this code using this tool named Gofmt?

Let us run the following Gofmt command:


The command is simple, gofmt -w <name of your go file>. Incidentally in the above example the file name is also gofmt.go, please don't get confuse. You can name it the way you like, a name that best describes your file. 

Now let us see the result of above command. The output, in the following image, looks more readable than the previous image.

We still have extra braces in Line# 6
If you carefully observe the above image, we still have a pair of extra braces in line# 6. To explicitly remove them we can use the following command:



For your implementation the only thing which changes here is the name of the go file, rest you can keep same. The following output now looks neat and well formatted as expected. 


Questions 

Try what happens when you type the following command.

1. gofmt filename.go
2. gofmt filename.go > newfilename.go
3. Can you use the gofmt tool to format all the go files inside a directory via a single command?

Answer

1. This will print the reformatted code but won't change the source file. If you want to change the source file you must introduce the flag -w , something like, gofmt -w filename.go (already shown, the 1st command of this post).

2. This will write the formatted code of source file filename.go into a new file named newfilename.go in the same directory.

Currently, I'm using LiteIDE and I'm happy to see that it already has Gofmt tool available from the toolbar as a menu option (Build > Gofmt). So there's an option for using Gofmt from the UI as well.

3. Yes. The same command that we used for files works for directory as well. We simply need to provide the directory path. It will be:

gofmt -w DirectoryPath. 

Example, if you've 3 files in a directory named src inside go directory in C drive and you want to format all of them in one shot, then you simply need to issue the following command:

gofmt -w go\src


References

http://blog.golang.org/go-fmt-your-code

No comments:

Post a Comment