How to use hot reload in Go

vitorvargas

Vitor Vargas

Posted on November 11, 2023

How to use hot reload in Go

When I took my first steps into studying Golang, I chose to use Chi to manage the routes of my first API. However, I faced the absence of an essential feature: hot reload. In an effort to enhance my development process, I delved into extensive research on the internet. Unfortunately, I didn't find simple solutions and encountered alternatives, such as running Go with nodemon, which made me somewhat uncomfortable. After all, why mix Go with Node?

That's when I came across taskfile.dev. Task is an automation tool designed to be more accessible than other options, such as GNU Make.

Developed in Go, Task is a simple binary with no other dependencies. This means you won't face complications during the installation process, making the use of this automation tool a straightforward experience.

To get started, it's crucial to perform the installation of taskfile. Follow the instructions in the manual available at https://taskfile.dev/installation/.

After a successful installation, we need to create a file to configure taskfile in your project. So, in the root directory of your project create the taskfile.yml file with the following content:

version: '3'

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

  :start:
    cmds:
      - task: :build
      - './dist/main'
    sources: 
      - ./*.go,
      - ./**/*.go
Enter fullscreen mode Exit fullscreen mode
  1. Taskfile Version: The first line specifies the version of the taskfile being used.

  2. Task Definitions:

    • :build: This task compiles the Go project. It uses the go build command to compile the source code located in ./main.go and saves the resulting executable in dist/main.
    • :start: This task depends on the :build task and then executes the generated executable (./dist/main). This provides a way to compile and start the application with a single command.
  3. Sources:

    • Both tasks have a list of sources indicating which files or directories should be watched to trigger task execution when changes occur. This is useful for the hot reload feature, allowing task to detect changes in source files and automatically execute related tasks.
  4. Execution Command:

    • The commands under the cmds key are the actual commands executed by the task. They are written as if they were standard terminal commands.

After configuring this taskfile.yml and installing task, you can use the command go-task :start -w --interval=500ms to compile and start your application, with the ability to hot reload whenever changes occur in the specified files. This process simplifies the development cycle.

TIP: If this doesn't work, try replacing go-task with task.

In summary, the integration of taskfile in Go projects provides an elegant solution for managing development tasks such as application compilation and execution, especially when it comes to the crucial need for hot reload. By adopting taskfile, you gain a straightforward automation tool based on Go, eliminating the need for more complex and mixed solutions. The configuration presented in taskfile.yml allows for easy compilation and startup of the application, while continuous monitoring of changes in source files facilitates automatic updates, providing a more efficient and complication-free development flow. By incorporating this approach into your workflow, you maximize productivity and maintain a focus on quality development rather than dealing with repetitive manual tasks.

💖 💪 🙅 🚩
vitorvargas
Vitor Vargas

Posted on November 11, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to use hot reload in Go
go How to use hot reload in Go

November 11, 2023