Book Image

Go Programming Blueprints

By : Mat Ryer
Book Image

Go Programming Blueprints

By: Mat Ryer

Overview of this book

Table of Contents (17 chapters)
Go Programming Blueprints
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Cleaning up, building, and running tests on save


Since the Go core team has provided us with such great tools as fmt, vet, test, and goimports, we are going to look at a development practice that has proven to be extremely useful. Whenever we save a .go file, we want to perform the following tasks automatically:

  1. Use goimports and fmt to fix our imports and format the code.

  2. Vet the code for any faux pas and tell us immediately.

  3. Attempt to build the current package and output any build errors.

  4. If the build is successful, run the tests for the package and output any failures.

Because Go code compiles so quickly (Rob Pike once actually said that it doesn't build quickly, but it's just not slow like everything else), we can comfortably build entire packages every time we save a file. The same is true for running tests, to help us if we are developing in a TDD style, and the experience is great. Every time we make changes to our code, we can immediately see if we have broken something or had an unexpected impact on some other part of our project. We'll never see package import errors again, because our import statement will have been fixed for us, and our code will be correctly formatted right in front of our eyes.

Some editors will likely not support running code in response to specific events, such as saving a file, which leaves you with two options; you can either switch to a better editor or write your own script file that runs in response to filesystem changes. The latter solution is out of scope for this book, instead we will focus on how to implement this functionality in a popular text editor.

Sublime Text 3

Sublime Text 3 is an excellent editor for writing Go code that runs on OS X, Linux, and Windows, and has an extremely powerful expansion model, which makes it easy to customize and extend. You can download Sublime Text from http://www.sublimetext.com/ and trial-use it for free before deciding if you want to buy it or not.

Thanks to DisposaBoy (see https://github.com/DisposaBoy), there is already a Sublime expansion package for Go, which actually gives us a wealth of features and power that a lot of Go programmers actually miss out on. We are going to install this GoSublime package and then build upon it to add our desired on-save functionality.

Before we can install GoSublime, we need to install Package Control into Sublime Text. Head over to https://sublime.wbond.net/ and click on the Installation link for instructions on how to install Package Control. At the time of writing, it's simply a case of copying the single, albeit long, line command, and pasting it into the Sublime console which can be opened by navigating to View | Show Console from the menu.

Once that is complete, press shift + command + P and type Package Control: Install Package and press return when you have selected the option. After a short delay (where Package Control is updating its listings), a box will appear allowing you to search for and install GoSublime just by typing it in, selecting it, and pressing return. All being well, GoSublime will be installed and writing Go code has just become an order of magnitude easier.

Tip

Now that you have GoSublime installed, you can open a short help file containing the details of the package by pressing command + ., command + 2 (the command key and period at the same time, followed by the command key and number 2).

Tyler Bunnell is another popular name in the Go open source community (see https://github.com/tylerb) and we are going to use his customizations to implement our on-save functionality.

Press command + ., command + 5 to open the GoSublime settings and add the following entry to the object:

"on_save": [
  {
    "cmd": "gs9o_open", 
    "args": {
      "run": ["sh", "go build . errors && go test -i && go test && go vet && golint"],
      "focus_view": false
    }
  }
]

Tip

Notice that the settings file is actually a JSON object, so be sure to add the on_save property without corrupting the file. For example, if you have properties before and after, ensure the appropriate commas are in place.

The preceding setting will tell Sublime Text to build the code looking for errors, install test dependencies, run tests, and vet the code whenever we save the file. Save the settings file (don't close it just yet), and let's see this in action.

Navigate to Choose File | Open… from the menu and select a folder to open—for now let's open our tooling folder. The simple user interface of Sublime Text makes it clear that we only have one file in our project right now, main.go. Click on the file, add some extra linefeeds, and add and remove some indenting. Then navigate to File | Save from the menu, or press command + S. Notice that the code is immediately cleaned up, and provided you haven't removed the oddly placed return statement from main.go, you will notice that the console has appeared, and it is reporting the issue thanks to go vet:

main.go:8: unreachable code

Holding down command + shift and double-clicking on the unreachable code line in the console will open the file and jump the cursor to the right line in question. You can see how helpful this feature is going to be as you continue to write Go code.

If you add an unwanted import to the file, you will notice that on using on_save you are told about the problem, but it wasn't automatically fixed. That's because we have another tweak to make. In the same settings file as you added the on_save property to, add the following property:

"fmt_cmd": ["goimports"]

This tells GoSublime to use the goimports command instead of go fmt. Save this file again and head back to main.go. Add net/http to the imports again, remove fmt import, and save the file. Notice that the unused package was removed and fmt was again put back.