Golang / GoLang Production Patterns and Web Standards Interview Questions
How does Go's module system work and what are the key commands?
The Go module system (introduced in Go 1.11, stable in Go 1.13) manages dependencies through a go.mod file. It replaced GOPATH-based dependency management and provides reproducible builds through cryptographic checksums in go.sum.
// go.mod anatomy module github.com/myorg/myservice go 1.22 require ( github.com/lib/pq v1.10.7 golang.org/x/sync v0.6.0 ) require ( // Indirect dependencies (transitive) github.com/some/dep v1.2.3 // indirect ) // Key commands: // Initialise a new module // go mod init github.com/myorg/myservice // Add or upgrade a dependency // go get github.com/lib/pq@v1.10.7 // go get github.com/lib/pq@latest // Remove unused and add missing dependencies // go mod tidy // Verify checksums // go mod verify // Vendor dependencies (for reproducible builds or air-gapped environments) // go mod vendor // go build -mod=vendor ./... // Replace directive: use local fork during development // replace github.com/myorg/lib => ../local-lib // Minimum Version Selection (MVS): // Go selects the MINIMUM version of each dependency that satisfies // all requirements in the module graph â deterministic, no implicit upgrades // Unlike npm/pip: go get explicitly upgrades, nothing upgrades silently
More Related questions...