Hot reloading in Go applications
Tags: go, hotreloading, webdev, fullstack
Published: 2024-04-26 08:25:11

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.

```bash
mkdir sample_go && cd sample_go
```

Next, initialize a Go module to manage project dependencies.

```bash
go mod init sample_go
```

Install the required dependency.

```bash
go get github.com/gin-gonic/gin
```

Create a sample application.

```go
package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "Hello world!",
        })
    })

    r.Run()
}
```

## Modd for hot reloading

[Modd](https://github.com/cortesi/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:

```bash
**/*.go {
    prep: go test @dirmods
}
```

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:

```bash
modd
```

> 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](https://github.com/cosmtrek/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:

-   [Modd documentation](https://github.com/cortesi/modd)
-   [Air documentation](https://github.com/cosmtrek/air)