Skip to content

Instantly share code, notes, and snippets.

@craicoverflow
Last active February 21, 2022 11:03
Show Gist options
  • Save craicoverflow/204d155278099a18877b42b3d5e6a772 to your computer and use it in GitHub Desktop.
Save craicoverflow/204d155278099a18877b42b3d5e6a772 to your computer and use it in GitHub Desktop.
Pre-commit Git hook for Go code
#!/bin/sh
# Run some pre commit checks on the Go source code. Prevent the commit if any errors are found
echo "Running pre-commit checks on your code..."
FILES=$(go list ./... | grep -v /vendor/)
# Format the Go code
go fmt ${FILES}
# Check all files for errors
{
errcheck -ignoretests ${FILES}
} || {
exitStatus=$?
if [ $exitStatus ]; then
printf "\nErrors found in your code, please fix them and try again."
exit 1
fi
}
# Check all files for suspicious constructs
{
go vet ${FILES}
} || {
exitStatus=$?
if [ $exitStatus ]; then
printf "\nIssues found in your code, please fix them and try again."
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment