github/gitignore · Go.gitignore
Go .gitignore 模板详解
说明 Go.gitignore 忽略二进制文件、测试二进制、覆盖率配置文件和 go.work 的原因,以及为什么需要另行拦截无扩展名的可执行文件。
Go.gitignore 处理构建出的二进制文件、测试与覆盖率产物,以及本地工作区文件。Go 把依赖下载到模块缓存($GOPATH/pkg/mod),因此仓库中不会出现像 Node 的 node_modules 那样庞大的目录,模板也很短。
go.mod 和 go.sum 不属于忽略对象。这两个文件记录模块的依赖和校验和,务必提交。
逐条规则详解
| 模式 | 忽略什么,为什么忽略 |
|---|---|
*.exe*.exe~*.dll*.so*.dylib | 二进制与库go build 的结果以及用 cgo 生成的共享库。它们需要按平台分别构建,因此应作为发布产物分发,而不是放进仓库。 |
*.test | 测试二进制用 go test -c 生成的测试可执行文件,在包名后面加上 .test。 |
*.outcoverage.**.coverprofileprofile.cov | 覆盖率与性能分析go test -coverprofile=coverage.out 之类的命令生成的结果,是每次运行都会变化的测量值。 |
go.workgo.work.sum | 工作区在本地同时开发多个模块时使用的文件,指向自己磁盘上的路径。go help work 文档也说明,一般不建议把 go.work 纳入版本控制。 |
.env | 环境变量存放本地配置和机密值的文件。示例文件请用其他名称(例如 .env.example)提交。 |
实践中的注意事项
- 在 Linux、macOS 上,
go build会生成无扩展名的可执行文件(模块名)。模板无法拦截它,因此请直接添加/myapp或/bin/之类的规则。 - 不要忽略
vendor/。有些团队用go mod vendor把依赖放进仓库,请按团队方针决定。 *.out会忽略所有名称以.out结尾的文件,如果测试期望值文件使用了这个扩展名,请像!testdata/**/*.out这样设置例外。- 如果使用 GoLand,请同时选择 JetBrains 模板。
原始模板
Go.gitignore
# If you prefer the allow list template instead of the deny list, see community template:# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore## Binaries for programs and plugins*.exe*.exe~*.dll*.so*.dylib# Test binary, built with `go test -c`*.test# Code coverage profiles and other test artifacts*.outcoverage.**.coverprofileprofile.cov# Dependency directories (remove the comment below to include it)# vendor/# Go workspace filego.workgo.work.sum# env file.env# Editor/IDE# .idea/# .vscode/
模板来源: github/gitignore/Go.gitignore @356fd7b (2026-09-11) · CC0-1.0