How to implement hot reloading in a Go application

This guide will explore what hot reloading is and why it is needed. It will also explore two libraries to help build hot reloading into your applications.

avatar

Demola Malomo

Apr 25 2024

3 min read

avatar

As someone who actively builds applications within the JavaScript / Node.js ecosystem leveraging frameworks like Next.js, Vue, Express.js, etc., hot reloading is usually built into the framework or needs to install a package to get it up and running.

While exploring the Go ecosystem, hot reloading seems like an afterthought in most of their popular frameworks. In fact, it became a source of concern for me and led me down the rabbit hole of finding possible solutions on how to include hot reloading in my Go applications.

This guide will explore what hot reloading is and why it is needed. It will also explore two libraries to help build hot reloading into your applications.

What is hot reloading?

Hot reloading is a technique used during application development, allowing you to observe updated changes to the source code in real-time without reloading the entire application. It preserves the application state, enabling you to continue working without losing the current state of the application. It allows you to view changes instantly, accelerating the development process and enhancing productivity.

What are the benefits of hot reloading?

The following are the benefits of enabling hot reloading in your applications:

  • Enhanced user experience
  • Fast development
  • Minimal context switching
  • Improved feedback loop
  1. Enhanced user experience: The ability to make changes to the codebase and see them in real-time improves the development process. With this capability, developers can rapidly prototype and design better applications.
  2. Fast development: With hot reloading, developers don't have to manually reload the application to see the latest changes. Development time is fast because changes are reflected immediately.
  3. Minimal context switching: A major source of distraction for developers is having to go back to the terminal to rebuild and run the application every time changes are made. However, with hot reloading, they can focus on the task without any interruptions or worries about their changes being reflected as updates occur in real-time.
  4. Improved feedback loop: Because changes are visible in real-time, developers receive immediate feedback and can easily fix bugs, identify where the application is broken, and consequently improve the development workflow.

How to implement hot reloading in a Go application

This section will explore two libraries that make hot reloading possible in a Go application:

  • Modd
  • Air

Before we explore how to use these libraries, let’s set up a sample Go project using the Gin-gonic framework.

Create a directory.

1mkdir sample_go && cd sample_go

Next, initialize a Go module to manage project dependencies.

1go mod init sample_go

Install the required dependency.

1go get github.com/gin-gonic/gin

Create a sample application.

1package main 2 3import "github.com/gin-gonic/gin" 4 5func main() { 6 r := gin.Default() 7 r.GET("/", func(c *gin.Context) { 8 c.JSON(200, gin.H{ 9 "message": "Hello world!", 10 }) 11 }) 12 13 r.Run() 14}

Modd for hot reloading

Modd is a library that makes hot reloading possible in Go applications. To use it, install it on your machine using the command below:

go install github.com/cortesi/modd/cmd/modd@latest

In your project directory, create a modd.conf file and add the snippet below:

1**/*.go { 2 prep: go test @dirmods 3}

The command above tells Modd to watch for changes to any .go file and run the changes on test directories.

To enable hot reloading, run the command below in your terminal:

1modd

In case you get an error about go: errors parsing go.mod: /project/path/ invalid go version '1.XX.X': must match format 1.23. Remove the Go semantic version stated inside the go.mod file. For example change go 1.22.1 to go 1.22

Air for hot reloading

Air is another library that enables hot reloading in Go applications. To use it, install it on your machine using the command below:

go install github.com/cosmtrek/air@latest

Next, initialize Air by running the command below:

air init

The command above will create a .air.toml file with default configurations to get your application up and running.

Finally, to enable hot reloading, run the command below:

air

Conclusion

Both libraries enable hot reloading and offer extensive configuration and flexibility regarding which files and folders should be excluded or ignored, pattern matching, environment setup, and much more. To learn more, you can check out their official documentation.

Useful resources:

Related posts