Go Module Init and Dependency Management¶
In the Go ecosystem, modules are the standard unit for managing dependencies and versioning. They define a collection of related Go packages that are versioned and distributed together^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Initialization¶
A Go module is initialized within the root directory of your project using the go mod init command^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
go mod init [module-path]
For example, running go mod init videos creates a new module file named go.mod^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. This file records the module's path and, later, its dependency requirements.
Adding Dependencies¶
Dependencies are managed explicitly during development. When external libraries are required, the go get command is used to fetch and add them to the module^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
For instance, to add the Redis client library:
go get github.com/go-redis/redis/v8
This command downloads the package, updates the go.mod file, and creates or updates the go.sum file to ensure cryptographic integrity of the dependencies^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Related Concepts¶
- Go Modules
- [[Dependency Injection]]
Sources¶
400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md