Package and Module#
Package#
A package is a way to group Go source files within a single directory. It is the unit of organization at the code level.
- Scope: All files in the same package share the same scope.
- Import: Code from other packages is accessed via the
importstatement. Imports are scoped to the package.
Module#
A module is a collection of related Go packages that are versioned together as a single unit. It handles versioning and dependency management.
- Definition: A directory tree of Go source code becomes a module when it contains a valid
go.modfile at its root. - Module Path: The
go.modfile defines the module path, which serves as the import path prefix for all packages within the module. - Dependency Management: It handles versioning and ensures reproducible builds by tracking dependencies and their checksums (in the
go.sumfile).
Initialization#
To create a new module, use the following command:
$ go mod init <MODULE_PATH>eg.
$ go mod init github.com/user/project