Effortless Hot Reloading in Golang: Harnessing the Power of Viper

fsnotify, and Callback Functions for Dynamic Configuration Updates

Adam Szpilewicz
6 min readMay 7, 2023
Photo by Mike van den Bos on Unsplash

In this post, we’ll create a simple HTTP server that serves a configurable welcome message. The server will hot-reload the configuration when the YAML file is changed. We use the following concepts: push mechanism for change emitting and callback functions, which are a powerful concepts in distributed systems and asynchronous programming. Let’s look what are we going to build.

Install Viper

First, make sure you have the Viper package installed. Run the following command in your terminal:

go get -u github.com/spf13/viper

Create a YAML configuration file

Create a config.yaml file with the following content:

server:B
Host: "localhost"
Port: 8080
WelcomeMessage: "Welcome to our hot-reloading server!"

Write the Go application

Create a main.go file with the following code:

--

--

Adam Szpilewicz

Backend software engineer working with golang and python @Rivery (https://rivery.io/). I like writting and reading about code and software engineering.