Golang / GoLang Basics Interview Questions
What is the Go module system? What do go.mod and go.sum contain?
Modules are Go's dependency management system (stable since Go 1.13). A module is a collection of related packages identified by a module path (typically a repository URL). The module's root contains a go.mod file that records the module path, the minimum Go version, and all required dependencies.
// Initialise a module // $ go mod init github.com/alice/myapp // go.mod â human-editable; commit to version control // âââââââââââââââââââââââââââââââââââââââââââââââââ // module github.com/alice/myapp // // go 1.22 // // require ( // github.com/gin-gonic/gin v1.9.1 // golang.org/x/sync v0.6.0 // ) // go.sum â machine-managed; commit to version control // Contains SHA-256 hashes of each downloaded module zip // Guarantees tamper-proof, reproducible builds // Key commands: // go get github.com/pkg@v1.2.3 â add / upgrade dependency // go mod tidy â remove unused, add missing deps // go mod download â pre-download all deps // go list -m all â list entire dependency graph // Minimum Version Selection (MVS): // Go ALWAYS picks the minimum version that satisfies all requirements. // Nothing upgrades silently â builds are reproducible. // Major version imports (breaking changes need new import path): // import "github.com/alice/pkg/v2" â v2 has breaking API changes
More Related questions...