Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Initializing a Go Module | Section
Go Modules and Package Management

bookInitializing a Go Module

Свайпніть щоб показати меню

To start using Go modules in your project, you need to initialize a module in your project's root directory. This is done with the go mod init command, which sets up the project to manage its dependencies using modules. The command takes a single argument: the module path. This path is usually the repository location where your code will be published, such as github.com/username/projectname, but it can be any name you choose for local projects.

By running go mod init, Go creates a go.mod file in the current directory. This file tracks important metadata about your project, including the module path, the Go version your project is using, and any dependencies your code requires. The module path is significant because it uniquely identifies your module and is used for importing your code from other projects.

main.go

main.go

go.mod

go.mod

copy
1234567
package main import "fmt" func main() { fmt.Println("Hello, Go Modules!") }

The go.mod file is the cornerstone of Go modules. It contains several key fields:

  • Module path: this is the unique identifier for your module, typically matching the repository path or a chosen local name;
  • Go version: this specifies the Go language version the module was initialized with, helping ensure consistent builds across environments;
  • Dependencies: as you add dependencies, they will be listed here with their module paths and versions.

The go.mod file is automatically updated by Go tools as you add, update, or remove dependencies. This makes it easy to track exactly which versions of external packages your project depends on, improving reproducibility and collaboration.

question mark

What is the main purpose of the go.mod file in a Go project?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 2
some-alt