This is a creation in Article, where the information may have evolved or changed.
We use Git to do version control of Go source, before we commit, we need to automatically format the code, and when unformatted, output some information, here is a script like this:
Note: This script originates from: Http://golang.org/misc/git/pre-commit
About the small article about cleaning Go code: An Introduction to Go Tools and clean Code
#!/bin/sh # Copyright: the Go Authors. All rights reserved. # Use of this source code was governed by a Bsd-style # license that can being found in the license file. # git gofmt pre-commit hook # to use, store as. Git/hooks/pre-commit inside your repository and make sure # It has execute permissions. # # This script does not handle file names, that contain spaces. gofiles=$ (git diff--cached--name-only--diff-filter=acm | grep '. go$ ') [-Z ' $gofiles] && exit 0 unformatted=$ (Gofmt-l $gofiles) [-Z ' $unformatted] && exit 0 # Some files is not gofmt ' d. Print message and fail. echo >&2 "Go files must is formatted with GOFMT. Please run: "for FN in $unformatted; Do echo >&2 "gofmt-w $PWD/$fn" done exit 1