Setting up Air for hot-reloading in Fiber Framework (Golang)

Oct 18, 2021
While learning how to build APIs in Golang, I came across the Fiber framework which is inspired by the express.js framework. Being a Javascript developer, this was the perfect choice for me to get started with building APIs in Golang.


After setting up the basic Fiber project, for any changes I made, I had to restart the go application every time. nodemon is a very common package we use for hot-reloading in the JS world so I started looking for its alternative in the Golang.


I quickly came across the air package. After some googling, I was quickly able to get up and running with hot-reloading in my Fiber app.
Let's install the air package globally
curl -sSfL | sh -s -- -b $(go env GOPATH)/bin
This will install air at <GOPATH>/bin/air. For me it was at /Users/overcompiled/go/bin/air.
After this, I will suggest you create an alias for air, so that you won't have to call the full path for air every time.
Since I'm using Mac, I added it to my .zshrc file.
alias air="~/go/bin/air" 
Now go to your Fiber project directory and run this command
air init
This will create a .air.toml file with the default configuration.
root = "."
tmp_dir = "tmp"

  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor"]
  exclude_file = []
  exclude_regex = []
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

  time = false

  clean_on_exit = false
Since I just want to create an API-based project, I removed the tpl, tmpl, and html from include_ext as they are required when you want to render templates (web pages) from your server.
Now just run
in your project and you are good to go. Change something in your project and send the API request to see the change reflect without restarting your Fiber application again and again.

© Nikit Singh 2020 - 2021