Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Created August 2, 2012 23:27
Show Gist options
  • Save jmoiron/3241916 to your computer and use it in GitHub Desktop.
Save jmoiron/3241916 to your computer and use it in GitHub Desktop.
Run this script in your directory to auto build whenever you modify a golang file
#!/bin/bash
cur=`pwd`
inotifywait -mqr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e modify ./ | while read date time dir file; do
ext="${file##*.}"
if [[ "$ext" = "go" ]]; then
echo "$file changed @ $time $date, rebuilding..."
go build
fi
done
@leonidlm
Copy link

Very useful script.

I modified it a bit, to allow providing the watch location as a command line parameter.

#!/bin/bash

LOCATION=${1:-./}

echo "watching location ${LOCATION}"

inotifywait -mqr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
   -e modify ${LOCATION} | while read date time dir file; do
    ext="${file##*.}"
    if [[ "$ext" = "go" ]]; then
        echo "${dir}${file} changed @ $time $date, rebuilding..."
        go build ${dir}${file}
    fi
done

@fclairamb
Copy link

Thank you for this.

I made a fork of your script here. I'm using it mostly for web development. Changes are:

  • Using "moved_to" instead of "modify" because most editors and IDEs use an atomic way of saving files (saving a temporary file than renaming it).
  • Killing and relaunching the process and each build
  • Displaying notifications with notify-send (so Linux only)
  • Building dependencies (go get) if some were built during last build
  • Using current dir and $GOPATH as monitored locations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment