go modules - ghdrako/doc_snipets GitHub Wiki

Go modules (go mod) is the dependency management system used in the Go programming language. It allows developers to track and manage their project's dependencies, ensuring that all necessary packages are available and version-controlled.

  1. go mod init - Initializes a new Go module in the current directory.
go mod init example.com/greetings
go mod init github.com/user/project

The go tool provides several commands that are related to modules.

  • go mod init - initializes new module in current directory
  • go mod tidy - adds missing and removes unused modules. It ensures that the go.mod and go.sum files are clean and up-to-date.
  • go mod download - downloads modules to local cache - downloads the modules needed by the project but does not install them globally.
  • go mod vendor - makes vendored copy of dependencies. This can be useful when you need to include all dependencies with the code for distribution.
  • go mod graph - prints module requirement graph - Displays the module dependency graph, showing how the modules depend on each other.
  • go mod verify - verifies dependencies have expected content - Verifies that the dependencies' checksums in go.sum match the downloaded content.
  • go mod why - explains why packages or modules are needed
  • go get - fetches a specific module version and updates the go.mod file accordingly. You can specify a particular version like @vX.Y.Z. There are additional commands related to Go modules.

The go list -m lists available modules. The go get installs dependencies and updates the go.mod file. The go build and go test commands add new dependencies to go.mod as needed.

go.mod File Structure:

    module: The module's name, usually a URL.
    go: The version of Go the module is compatible with.
    require: Lists all the module dependencies with their respective versions.
    replace: Allows you to replace a module with a different version or a local path.
    exclude: Prevents specific module versions from being used.

example

go
module github.com/user/project

go 1.20

require (
    github.com/some/dependency v1.0.0
    github.com/another/dependency v2.3.1
)

replace github.com/another/dependency v2.3.1 => github.com/another/dependency v2.3.2
go mod edit --replace external_folder_module_name= ../pathItsLocated

$ go mod edit -replace github.com/pselle/bar=/Users/pselle/Projects/bar

go vet - kompilacja na sucho

W dokumentacji biblioteki sa informacje jaka wersja biblioteka jaka ma podatnosc i ostrzega przed tym. Sumy kontrolne bibliotek w go serverze.

⚠️ **GitHub.com Fallback** ⚠️