Automatic Reloading in Go

Dinesh Silwal
wesionaryTEAM
Published in
2 min readMar 5, 2021

--

When i first started my journey on golang, i could not found an easy way to setup auto reloading. So the issue was every time i make changes to my source code, i had to kill the existing server, restart server again and then test API with postman.

Definitely, this process is cumbersome and headache 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.

I referred quite a lot of articles but could find proper solution for this like in Nodejs tools: Nodemon that helps to develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.or Shotgun or Guard when working with Ruby.

but recently i found a article that would be really helpful to implement reloading process in automated way.

There is a short Bash script file to automatically reload Go programs.The script acts as a light wrapper around go run, stopping and restarting it whenever a .go file in your current directory or $GOPATH/src folder is saved.

You can receive Bash script file from : https://github.com/alexedwards/go-reload along with required instructions to install commands or follow the steps as well.
Install inotify-tools and clone this repository:

$ sudo apt-get install inotify-tools
$ git clone https://github.com/alexedwards/go-reload.git
  1. Make the script executable and move it to somewhere on your system path. For example:
$ cd go-reload
$ chmod +x go-reload
$ sudo mv go-reload /usr/local/bin

Use the script in place of go run. For example:

$ go-reload main.go
== Go-reload
>> Watching directories, CTRL+C to stop

By default go-reload watches for changes to *.go files only. You can change this behaviour so that all file types are watched by using the -a flag. For example:

$ go-reload -a main.go
== Go-reload
>> Watching directories, CTRL+C to stop

You are ready to go then….and Thank you !!!!

--

--