Modules, Packages, and Imports Flashcards
What is a repository in Go?
A repository stores the project’s source code and is placed in a version control system.
What is a module in Go?
A module is a bundle of Go source code, distributed and versioned as a unit, stored in a repository, and contains one or more packages.
What is a package in Go?
A package is a directory of source code that provides organization and structure to a module.
How many modules should be in a repository?
One module per repository to track versions separately for different modules.
How is a package in Go similar to a package in Node.js?
In Node.js, “package” is similar to Go’s “module.”
What does the go.mod file do?
It converts a directory tree into a module, specifies the Go version, and includes module dependencies.
What command initializes a go.mod
file?
The command go mod init MODULE_PATH
initializes the go.mod
file, making the directory the root of a module.
What is the purpose of the require
directive in go.mod
?
The require
directive lists the modules your module depends on, including direct and indirect dependencies.
What is the difference between importing and exporting in Go?
Importing grants access to exported identifiers, and exporting makes identifiers available outside the package, starting with a capital letter.
How do you import a package in Go?
You import external packages using their module paths, ensuring at least one identifier from the imported package is used.
How should you name your packages?
Package names should be descriptive, typically nouns, while functions/methods are usually named with verbs.
What is an internal package?
An internal package is a special package used to share code within a module but not accessible outside of its parent and sibling packages.
How can circular dependencies be avoided in Go?
Circular dependencies can be avoided by merging dependent packages.
When should the init
function be avoided in Go?
The init function should be avoided unless necessary, as it sets up code without being explicitly called.
How do you import third-party code in Go?
Third-party code is imported by specifying the location and version in the go.mod
file, and go get
is used to add dependencies.
What is Semantic Versioning (SemVer)?
SemVer is a versioning system with major, minor, and patch increments to indicate compatibility and bug fixes.
What is pkg.go.dev used for?
pkg.go.dev indexes and provides documentation for Go modules, making it useful for discovering third-party modules.
Do Go modules require a central repository?
No, there is no need for a central library repo.
How do you version a Go module?
Use semantic versioning for releases, with pre-releases tagged like -beta1 (e.g., v1.3.4-beta1).