Hot Reload for Golang with Go Air
When developing in Golang (also known as Go), like in any language speed and efficiency are important for Developer experience, and to ensure we can ship fast! One tool that can make the development process better is Air—a live reloading tool for Go applications. In this post, we’ll explore how to install, configure, and use Golang Air to boost your development workflow.
What is Golang Air?
Air is a live reloading tool designed for Golang developers. It automatically reloads your Go application whenever you make changes to your codebase. This means you don’t have to stop and restart your server manually after every change, resulting in a much better and smoother development process.
Why Use Air for Golang Development?
While Go’s speed and simplicity make it a powerful language, the traditional development process often involves manually restarting the application after code changes. This is tedious, and really impacts the development experience.
Here’s where Air comes in handy. It gives you the following:
- Automatic Reloading: Whenever you save a Go file, Air detects the changes and instantly reloads the server.
- Efficient Workflow: No more stopping and restarting the Go server. Air ensures that your changes are reflected in real-time.
- Developer Experience: Move faster by focusing on writing code rather than restarting your application.
With Air, you can achieve faster turnaround times and focus more on the business logic rather than having to keep stopping and starting.
How to Install Golang Air
Thankfully, getting started with Air is easy! Follow these steps to install and set it up in your Golang project.
Step 1: Install Air
First, you’ll need to install Air using Go’s package manager. Simply run the following command in your terminal:
go install github.com/cosmtrek/air@latest
If you’re using Homebrew on macOS, you can do this:
brew install cosmtrek/tap/air
Once installed, you can check if it’s working by typing:
air -v
This should output the current version of Air installed.
Step 2: Set Up Air in Your Go Project
Once you’ve installed Air, you need to configure it for your Golang project.
- Navigate to your project’s root directory.
- Run the following command to generate a default
.air.toml
configuration file:
air init
- This will create a
.air.toml
file, which contains settings you can change - things like directories to watch, file extensions to monitor, and build commands. By default, it should work pretty well for you out of the box, but you can find out about more configuration options here.
Step 3: Running Your Golang Project with Air
Once everything is set up, you can now start using Air in your Go project. Run the following command from your project directory:
air
Air will now monitor your files and reload the application automatically whenever you make changes to the Go code. Any changes to files will be detected almost straight away, and the server will refresh. If there are any errors in your change, these will also show up in the console.
Advanced Air Configuration Options
The default configuration for Air works well for most use cases, but you can further customize it if you’re doing something a little different. Here are some of the main customizations I have made from time to time.
Excluding Files from Watching
If your project contains certain directories or files you don’t want to trigger a reload (e.g., documentation, image assets), you can exclude them like follows:
[watcher]
ignore = ["assets/*", "docs/*"]
This ensures that changes in these directories won’t trigger an application reload.
Setting Environment Variables
You can configure environment variables for different environments, such as development or staging, by adding the following to your air.toml
:
[dev]
cmd = "go run main.go"
bin = "tmp/main"
[env]
GOPATH="/your/custom/gopath"
Wrapping up
Using Golang Air for live reloading is a game changer for your speed and efficiency when developing Go applications. By eliminating the need for manual restarts, Air helps you focus on what really matters—writing quality Go code.
Whether you’re working on a large-scale application or a simple project, integrating Air into your development process can save time and enhance productivity.
Happy coding! If you enjoyed this blog, consider signing up for our newsletter. In return, you’ll get a list of companies using Go in production who are hiring.