Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Transitive Dependencies | Section
Go Modules and Package Management

bookTransitive Dependencies

Swipe um das Menü anzuzeigen

When you add a dependency to your Go module, you are introducing a direct dependency—something your code explicitly imports. However, dependencies often come with their own dependencies, which your code does not import directly. These are called transitive dependencies. In Go, transitive dependencies are tracked automatically, ensuring your project always has all the code it needs to build and run.

The go.mod file keeps track of both direct and indirect dependencies. When you run go mod tidy or build your project, Go analyzes your imports and those of your dependencies, updating go.mod to include all required packages. The go.sum file records checksums for every version of every dependency, direct or transitive, used by your project. This ensures builds are reproducible and secure.

main.go

main.go

go.mod

go.mod

go.sum

go.sum

copy
1234567891011
package main import ( "fmt" "github.com/google/uuid" ) func main() { id := uuid.New() fmt.Println("Generated UUID:", id) }

In the example above, your project directly requires the github.com/google/uuid package. However, uuid itself depends on another package, github.com/pborman/uuid. This second dependency is a transitive dependency: your code never imports it directly, but it is needed for the code you do use. The go.mod file may not always list transitive dependencies; instead, they are recorded in go.sum to ensure integrity. If a transitive dependency is required by multiple direct dependencies or is referenced by your code, it might appear in go.mod with the // indirect comment.

The indirect keyword in go.mod signals that a dependency is not imported directly by your code, but is necessary for some direct dependency to function. This helps you distinguish between packages you rely on explicitly and those included only because your dependencies need them. Managing indirect dependencies is important for keeping your project tidy and understanding your module's dependency graph.

question mark

Which statement best describes the difference between direct and indirect dependencies in Go modules?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 7
some-alt