Git hooks for GO pre-commit format by gofmt.
This script will run goimports
and go vet
to format and validate the files.
- Clone this repo
- Set executable
sudo chmod +x $PATH_TO_REPO/git-hooks/pre-commit
- Change to target project folder
cd $PATH_TO_PROJECT
- Config project git
git config core.hooksPath $PATH_TO_REPO/git-hooks/
- Install goimports if not exist:
- Check with
which goimports
- GO version < 1.17
go get golang.org/x/tools/cmd/goimports
- GO version >= 1.17
go install golang.org/x/tools/cmd/goimports
- Set PATH env
-
Edit
~/.bashrc
(or~/.zshrc
based on your shell) -
Add following lines
export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
-
- Check with
- Copy
$PATH_TO_REPO/git-hooks/pre-commit
into$PATH_TO_PROJECT/.git/hooks/
- Execute the file
pre-commit
Wrong format:
package test
import "stringsx"
import "fmt"
import "os"
func Title(s string) {
fmt.Println(strings.Title(s))
}
func test()
{
Title("hello world")
}
Formatted:
package test
import (
"fmt"
"strings"
)
func Title(s string) {
fmt.Println(strings.Title(s))
}
func test() {
Title("hello world")
}
git config core.hooksPath
only available for git version > 2.9