Automatically Reloading Your Golang App

I’m a huge fan of Grunt and love my JavaScript workflow with unit tests (end to end testing on occassion) and live-reload. Sure, I could use Gulp as well, but I try not to get hung up on every new flavor of the week. I’m sure I’ll get into that good stuff sometime soon and will be sure to blog about it.

Great, but what about Go? I love Go and the tooling around it is aswesome, but I’m missing Grunt. Sure, I could probably go ahead and setup Grunt to watch files and execute commands just the same…But I don’t want Node.js mixed with my Go project. That sounds just a bit silly.

I also want something fast and flexible. I may run Go with different files from time to time and setting this all up with Grunt is a little time consuming.

So like with anything else, I hit Google and started researching. I found a neat little script called, “go-reload” which is great if you’re running Ubuntu. It uses inotify which OS X doesn’t have. This lead me to installing fsevents-tools, but it still didn’t work. I suppose it might with some more effort, but it didn’t seem to be a drop-in replacement.

Back to continue through more search results… Ruby to the rescure! …Words I don’t often use. It seems there’s a Ruby gem that’s easily installed to do what I want. It’s called rerun.

Once installed, I could very easily issue the command rerun --pattern '**/*.{go}' go run main.go and boom! It launched my application and would relaunch it if I edited and saved any .go file.

Of course I should note that unless your Go app runs indefinitely, a tool like this doesn’t make much sense. Many of my apps expose APIs so they are always listening on some port. For this type of work, a tool like rerun really makes development easy. It’s especially handy when you’re building many APIs or microservices that need to all run and talk to each other as you develop.

Now the benefit here over Grunt is that you aren’t polluting your project with tooling. Since rerun is installed globally, there’s nothing to add to .gitignore and no extra install steps. Once you have rerun installed, it works for all your projects.

Tags// , ,
comments powered by Disqus