How to set up live reloading Golang

Live Reloading

Live Reload reloads or refreshes the entire go app when a file changes. For example, if you were four links deep into your navigation and saved a change, live reloading would restart the app and load the app back to the initial route.

Benefits of Live Reload

  1. Increased Development Speed - Reduces downtime caused by repeatedly stopping and starting the server.
  2. Faster Debugging - Enables quick testing of changes and immediate feedback.

How to Enable Live Reload in Go

Air is a powerful live-reloading CLI utility for developing Go applications. It detects changes on Go files and automatically reloads the server, ensuring that your changes are reflected in real-time. This enhances the developer experience by allowing them to focus on coding rather than manually restarting the application.

Installation and Setup Guide for Air

  1. Install via:

    Run the following command to install Air:

      go install github.com/air-verse/air@latest
    
  2. Open Your Project Directory and Initialize .air.toml:

    • Navigate to your project path:

      cd /path/to/your_project
    
    • Initialize the Air configuration file:

      air init
    

🔗 After running the above, open the .air.toml file and adjust the settings as needed.

Troubleshooting

  1. Air Not Rebuilding the Project After Moving Files

    If you move the Go files to subdirectories, Air may stop detecting changes.

Fix 🛠️

Modify the cmd parameter in the .air.toml file to specify the correct location of main.go.

cmd = "go build -o ./tmp/main ./cmd/web/."

Update the .include_dir setting to ensure Air watches the correct directories:

include_dir = ["cmd", "internal", "ui"]

Note: Do not use a leading ./ in the directory names.

Example Project Structure:

cmd/
  web/
internal/
ui/
 html/
   pages/
   partials/
 static/
   css/
   img/
   js/
.air.toml
.gitignore
go.mod
go.sum

air.toml file:

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"


[build]
  args_bin = []
  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ./cmd/web/."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = ["cmd", "internal", "ui"]
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  post_cmd = []
  pre_cmd = []
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  silent = false
  time = false

[misc]
  clean_on_exit = false

[proxy]
  app_port = 0
  enabled = false
  proxy_port = 0

[screen]
  clear_on_rebuild = false
  keep_scroll = true



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Running LLMs Locally with Open Web UI and Ollama
  • Google Kubernetes Engine
  • Intro to GCP Cloud Shell and gcloud
  • Creating A Build Tool Using Go
  • Using Tiled with Kaplay