github/gitignore · Go.gitignore

Go .gitignore 模板详解

说明 Go.gitignore 忽略二进制文件、测试二进制、覆盖率配置文件和 go.work 的原因,以及为什么需要另行拦截无扩展名的可执行文件。

在生成器中打开 与 JetBrains, VisualStudioCode, macOS, Linux 一起打开

Go.gitignore 处理构建出的二进制文件、测试与覆盖率产物,以及本地工作区文件。Go 把依赖下载到模块缓存($GOPATH/pkg/mod),因此仓库中不会出现像 Node 的 node_modules 那样庞大的目录,模板也很短。

go.modgo.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)提交。

实践中的注意事项

原始模板

Go.gitignore规则 13 行
# 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

常搭配使用的模板

其他模板详解