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
- 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.
- 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.
- 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.
- 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.
mkdir sample_go && cd sample_go
Next, initialize a Go module to manage project dependencies.
go mod init sample_go
Install the required dependency.
go get github.com/gin-gonic/gin
Create a sample application.
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 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:
**/*.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:
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 thego.mod
file. For example changego 1.22.1
togo 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:
Top comments (0)