Summary: in this tutorial, you will learn how to set up live reload for Go programs with Nodemon.
When developing Go programs, you often change your code and want those changes to be reflected immediately in the running program.
Instead of manually stopping and restarting your Go program each time you make a code change, you can utilize a tool like nodemon
to automate this process.
In this tutorial, we’ll walk you through the steps to set up live reloading for a Go program using nodemon
.
Step 1. Install Node.js on your computer
First, visit the Node.js official website.
Second, download and install the suitable version for your operating system.
Third, open your terminal and verify that Node.js and npm are installed correctly by running the following commands:
node -v
npm -v
These commands should display the installed versions of Node.js and npm.
Note that npm stands for Node Package Manager, which allows you to install Node.js modules from the NPM repository.
Step 2: Install Nodemon
Nodemon is a tool that monitors your GO source files and automatically restarts the GO program when it detects changes.
To install Nodemon, open your terminal and install nodemon
globally using the following npm install
command:
npm install -g nodemon
The -g flag instructs the command to install nodemon globally on your computer. This means that you can access the nodemon command from any directory.
Step 3: Creating Up Your Go Program
Here’s the Hello, World! program. If you haven’t created it, here’s the source code.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Code language: Go (go)
Note that you need to store the code in the main.go file under the hello-world directory.
Step 4: Test Live Reload
First, open your terminal and navigate to the hello-world directory:
Second, run the following command to perform live reloads of the main.go program:
nodemon --exec go run main.go --signal SIGTERM
Code language: CSS (css)
In this command:
--exec go run main.go
: This instructsnodemon
to execute the commandgo run main.go
whenever changes are detected.--signal SIGTERM
: This option instructsnodemon
to send aSIGTERM
signal to the Go process when it needs to restart. It allows the Go program to handle a graceful shutdown if necessary.
With nodemon
running, you can make code changes to your main.go
file and watch the output message. For example, you can change the message to something like Hello, Go!
fmt.Println("Hello, Go!")
Code language: JavaScript (javascript)
Save the file, and you’ll see that nodemon
automatically detects the change, stops the running Go process, and starts it again with the updated code.
Step 5: Create a Nodemon Configuration File (Optional)
If you want to customize nodemon
, you can create a nodemon.json
configuration file in the root of your project with the following code:
{
"watch": ["*.go"],
"exec": "go run main.go",
"signal": "SIGTERM",
"ext": "go"
}
Code language: JSON / JSON with Comments (json)
In this file:
"watch": ["*.go"]
: watch all.go
files in the current directory."exec": "go run main.go"
: execute thego run main.go
command when nodemon detects the code changes in any.go
files."signal": "SIGTERM"
: The signal to send to the process when restarting."ext": "go"
: File extensions for the nodemon to monitor.
After having this file, you can simply run the nodemon
without any arguments because it’ll use the settings from nodemon.json:
nodemon
Summary
- Use
nodemon
to auto-reload your Go program whenever you make changes.