How to Detect File Changes in Golang

Image by deer

Go has quickly become one of my favorite languages to write in. I often find my self smiling at how easy Go makes almost every task. Detecting file changes is no exception. Using the filepath package from the standard library along with fsnotify, makes this a fairly simple task.

Watching single files or directories

Out of the box, fsnotify provides all the functionality needed to watch a single file or directory:

Watching nested files and directories

Unfortunately fsnotify doesn’t support recursion, so filepath will have to fill in the gaps:

Since fsnotify is able to watch all the files in a directory, filepath is used to walk the entire document tree looking only for directories to attach watchers to.

Husband, father, and tech enthusiast. I’m passionate about finding, using, and creating awesome stuff. Currently creating awesome stuff at https://getdivvy.com/

Husband, father, and tech enthusiast. I’m passionate about finding, using, and creating awesome stuff. Currently creating awesome stuff at https://getdivvy.com/