Skip to content

Gaper: rebuild and restart a Go program on crash or file change

Gaper is a command-line tool that builds your Go program, runs it, and rebuilds and restarts it when a watched file changes or when the program exits. It is the Go equivalent of nodemon or node-supervisor, and its README says it is aimed at development only. It is not a production process manager: for that, use systemd, a container restart policy, or similar.

You don’t import gaper into your code. You run it from the project directory in place of go run.

With Go 1.17 or later, install the binary with go install:

Terminal window
go install github.com/maxcnunes/gaper/cmd/gaper@latest

This puts gaper in $(go env GOPATH)/bin, so make sure that directory is on your PATH. Prebuilt binaries are also on the releases page.

From the root of a main package, run gaper with no arguments:

Terminal window
gaper

With the defaults, gaper:

  • builds the package in . into a binary named after the current directory;
  • runs that binary;
  • polls .go files every 500 ms and rebuilds and restarts the program when one changes;
  • restarts the program whenever it exits, whether it crashed or not;
  • ignores hidden files and folders, *_test.go files and the vendor folder.

All configuration is done with command-line flags; there is no config file or environment variables.

FlagWhat it does
--build-pathPath to the program’s source (default .)
--bin-nameName of the binary gaper builds
--build-argsExtra arguments for go build
--program-argsArguments passed to your program
-w, --watchFiles or folders to watch (repeatable, globs allowed)
-i, --ignoreFiles or folders to ignore (repeatable, globs allowed)
-e, --extensionsFile extensions to watch (default go)
-p, --poll-intervalPolling interval in milliseconds (default 500)
-n, --no-restart-onerror, exit or success (see below)
--disable-default-ignoreAlso watch hidden files, tests and vendor
--verbosePrint gaper’s own log messages

--no-restart-on controls what happens when your program exits on its own:

  • error: don’t restart when the program fails; a clean exit (code 0) still restarts. In v1.1.0 the check is for exit code 1 specifically, so other non-zero codes still trigger a restart.
  • success: don’t restart on a clean exit; crashes still restart.
  • exit: never restart automatically. Gaper still rebuilds and restarts on file changes.

A typical setup for a web server whose main package lives in cmd/server, and which also serves templates and static files:

Terminal window
gaper \
--build-path cmd/server \
--bin-name build/api-dev \
-w . -e go -e html -e css \
--program-args "-port 8080" \
--no-restart-on exit

When adding a directory to --watch or --ignore, end it with a / (for example build/). Otherwise the pattern can also match files that start with the same name, such as build_settings.go.

  • Gaper polls the file system rather than using file system events, so very large trees with a short --poll-interval cost some CPU.
  • Restarting on every exit is the default. If your program is a one-shot CLI rather than a server, pass --no-restart-on success or it will run in a loop.
  • There is no backoff or retry limit. A program that crashes on startup is rebuilt and restarted over and over until you fix it. A Go panic exits with code 2, which --no-restart-on error does not catch, so use --no-restart-on exit if the loop gets noisy.
  • If a rebuild fails, gaper logs the compiler error and keeps watching; the next file change triggers another build.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)