Setting up hot reloading for golang application

backend3 Min to Read20 Jun 20

When i first started working on golang, i could not found an easy way to setup auto reloading. I was working on backend server development, so every time i made changes to source code, i had to kill the existing server, restart server again and then test the api with postman.

Definitely, this process is cumbersome to do. Whenever we save the code changes, we want our old server process to be killed automatically and start new one with new code changes, so that we can test our changes immediately.

Overview

  • will setup simple http server using inbuilt api of golang.
  • will install reflex, that help us setting up hot reloading.
  • will setup make file to contain command for hot reload.
  • will test our setup

Setting up simple demo server

Below is the code snippet for simple http server in Go. Whenever any request comes, it returns Hello World message.

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello World")
	})
	http.ListenAndServe(":8080", nil)
}

Installing reflex

Reflex is tool written in Go that watches for file changes and rerun a command when certain file changes. See this link to know more about Reflex and its usages. Install Reflex using below command.

go get github.com/cespare/reflex

Setting up makefile

Every time, typing long command on terminal to build or run Go application is very tedious. So we use makefile to kind of alias to short command for long command.

Makefile is used for other programming language also like c, c++. In order to run makefile, we need make tool, which are pre installed on most of the linux/unix machine.

In below makefile, we have declared three task build, run and watch. Save the makefile to your project root directory then run:

  1. make build: to compile source to executable
  2. make run: to run executable, here run is dependent on build, so it first build and then run the executable
  3. make watch: to watch for any go file changes, on change it automatically kill the old server, compile and run with new changes.
build:
	go build -o server main.go

run: build
	./server

watch:
	ulimit -n 1000 #increase the file watch limit, might required on MacOS
	reflex -s -r '\.go$$' make run

On Mac, you can watch around max 256 file at a time, to see the current limit run ulimit -a and look for file descriptors like in below output.

-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       2784
-n: file descriptors                256

If you are watching for go file changes more than current limit then you might need to increase the limit using command ulimit -n 1000. Same command can be put as part of makefile for convenience.

Testing our setup

To test, first run make watch command from terminal then make any code changes in main.go file and save that file. You should see your code getting automatically compiled and run with new changes on terminal. Now you can test your server with api tester tool like Postman , Insomnia or curl

Reference

source code in git repo
video demo about setting up hot reloading

If you loved this post, Please share it on social media.