Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Deep Dive into go.mod | Section
Go Modules and Package Management

bookDeep Dive into go.mod

Glissez pour afficher le menu

Go modules use the go.mod file as the foundation for tracking your module’s identity, dependencies, and specific configuration. Several key directives appear in this file, each serving a distinct purpose:

  • The module directive defines the module path—the import path by which your module is referenced;
  • The go directive specifies the Go language version your module is intended to work with;
  • The require directive lists dependencies needed by your module, including their versions;
  • The replace directive allows you to substitute one module path and version with another, which is especially useful for local development or testing unpublished changes;
  • The exclude directive tells the Go toolchain to ignore a specific version of a module, preventing its use even if it is referenced elsewhere.

Understanding these directives is crucial for managing how your module interacts with its dependencies and the wider Go ecosystem.

go.mod

go.mod

main.go

main.go

copy
123456789101112
module github.com/example/myapp go 1.21 require ( github.com/example/utility v1.2.0 github.com/example/legacy v0.9.0 ) replace github.com/example/utility v1.2.0 => ../utility-local exclude github.com/example/legacy v0.9.0

The replace directive is especially powerful for local development and testing. By redirecting a dependency path to a local directory, you can experiment with changes to that dependency without needing to publish a new version or push updates to a remote repository. This makes it easy to iterate quickly on both your main project and its dependencies, ensuring that your changes work together as expected before making them public.

question mark

What is the main purpose of the replace directive in a go.mod file?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 9

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Section 1. Chapitre 9
some-alt