Featured image of post How to use hot reload in Go

How to use hot reload in Go

When I started developing my first API with Golang, I missed an essential feature: hot reload. Without it, every change in the code required recompiling and manually restarting the server, which quickly became a tedious process.

In search of something to solve this problem, I dove into online research and found a few solutions, such as using the Air library or Nodemon. However, neither made me comfortable. I didn’t want to add another library to my project (in the case of Air), and Nodemon, which is written in JavaScript and depends on NPM, didn’t appeal to me either. After all, I wanted something more aligned with the Go ecosystem.

That’s when I discovered Task. Task is an automation tool developed in Go that, besides being extremely simple to use, does not require any extra dependencies in the project. This means that, being a binary installed on your machine, you can use it without adding anything to your code or modifying its structure. For me, it was the perfect solution!

Installing and Configuring Task

First, you need to install Task. The instructions are in the official documentation: https://taskfile.dev/installation/. After installation, it’s time to configure the taskfile.yml in your project. This file is where you define the tasks that Task will execute. Here’s a basic example to enable hot reload:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
version: '3'

tasks:
  :build:
    cmds:
      - 'go build -o dist/main ./main.go'
    sources:
      - ./*.go
      - ./**/*.go

  :start:
    cmds:
      - task: :build
      - './dist/main'
    sources: 
      - ./*.go
      - ./**/*.go

How taskfile.yml Works

The taskfile.yml file is quite simple to understand:

  • :build: This task compiles the source code using the go build command and generates a binary (dist/main).
  • :start: This task depends on :build. It first compiles the code and then executes the generated binary.
  • Sources: Here, you define which files Task should monitor for changes. Whenever one of these files is modified, Task recompiles and restarts the server.

After configuring the taskfile.yml, you can run the command:

1
go-task :start -w --interval=500ms

This activates change monitoring in the code and keeps the server always updated with the changes.

Tip: If the go-task command doesn’t work, try just task.

Why I Chose Task?

Task is a lightweight, practical tool that is completely aligned with the Go ecosystem. Being an independent binary, it does not add dependencies to the project and does not require complicated configurations. Additionally, it is a flexible solution that is not limited to just hot reload, allowing for the configuration of various other automations in the project. After trying out several options, Task proved to be the ideal solution for me. It solves the hot reload problem simply and efficiently, without relying on external tools or adding libraries to the project. With the right configuration, as I showed above, you can have a much more practical and organized development workflow.