0%

go fmt && goimports

go 项目中多人代码格式化和import package 格式不一致,使用githooks统一代码风格.

配置 .githooks目录

1
2
3
4
5
.PHONY: init
init:
@git config core.hooksPath .githooks
@go get golang.org/x/tools/cmd/goimports@v0.1.5
@echo "githooks 配置成功"

配置 pre-commit.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
branch="$(git rev-parse --abbrev-ref HEAD)"

if [ $branch = master ] || [ $branch = dev ];then
echo "当前分支 $branch 不支持提交操作"
exit 1
fi

for file in $(git diff --cached --name-only --diff-filter=ACMRTUXB | grep "\.go")
do
echo "(gofmt) $file"
gofmt -w $file
goimports -w $file
git add "$file"
done