Go (Golang) .gitignore Configuration
Minimal and effective .gitignore for Go projects. Covers compiled binaries, vendor directory decisions, test output, and Go-specific build artifacts.
Detailed Explanation
Go projects tend to have a simpler .gitignore than most languages because Go does not generate bytecode caches or require virtual environments. However, there are still important patterns to understand.
Essential Go ignore patterns:
# Compiled binaries
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary and output
*.test
*.out
coverage.html
coverage.txt
# Build output
/bin/
/dist/
# Go workspace
go.work.sum
# Dependency directory
vendor/
Compiled binaries:
Go compiles to native machine code — there is no node_modules or __pycache__ equivalent. The output is a single binary file. If your project is named myapp, running go build produces a myapp (or myapp.exe on Windows) binary. Ignore these with explicit names or place build output in a bin/ directory.
The vendor/ directory decision:
Go modules (go.mod + go.sum) replaced the old GOPATH and vendoring approach. Today you have two legitimate choices:
- Ignore
vendor/(modern default) — Dependencies are cached in$GOPATH/pkg/mod/and downloaded viago mod download. Thego.sumfile ensures integrity. This is cleaner and recommended for most projects. - Commit
vendor/— Some organizations require vendoring for air-gapped environments or to ensure builds work even if a module is deleted from the internet. If vendoring, do NOT addvendor/to.gitignore.
Test artifacts:
*.test— Compiled test binaries created bygo test -c.*.out— CPU and memory profiling output fromgo test -cpuprofileorgo tool pprof.coverage.html— HTML coverage reports fromgo tool cover.
What to always commit:
go.mod— Module definition and direct dependency declarations.go.sum— Cryptographic checksums of all dependencies (direct and transitive). This is your lockfile.
Pro tip: Go builds are hermetic by design. With go.mod and go.sum committed, anyone can reproduce your build exactly. The .gitignore mainly just keeps compiled binaries and test output out of the repo.
Use Case
A Go microservices team with 12 services in a monorepo needs a consistent .gitignore strategy for handling binaries and deciding whether to vendor dependencies.