Установка пакетов
Last updated
Last updated
Был такой эксперимент — , но он был закопан в пользу механизма модулей — .
A module is a collection of packages that are released, versioned, and distributed together. Modules may be downloaded directly from version control repositories or from module proxy servers.
A module is identified by a , which is declared in a , together with information about the module’s dependencies. The module root directory is the directory that contains the go.mod
file. The main module is the module containing the directory where the go
command is invoked.
Each package within a module is a collection of source files in the same directory that are compiled together. A package path is the module path joined with the subdirectory containing the package (relative to the module root). For example, the module "golang.org/x/net"
contains a package in the directory "html"
. That package’s path is "golang.org/x/net/html"
.
Определяет рабочую область — набор go модулей (по умолчанию, текущая директория — активная рабочая область)
Для работы с workspace используем команды:
Или golang.org/x/mod/modfile
для выполнения подобных изменений программно.
All commands that load information about packages are module-aware. This includes:
go build
go fix
go generate
go get
go install
go list
go run
go test
go vet
These commands accept the following flags, common to all module commands.
The -mod
flag controls whether go.mod
may be automatically updated and whether the vendor
directory is used.
-mod=readonly
tells the go
command to ignore the vendor
directory and to report an error if go.mod
needs to be updated.
-mod=vendor
tells the go
command to use the vendor
directory. In this mode, the go
command will not use the network or the module cache.
The -modfile=file.mod
flag instructs the go
command to read (and possibly write) an alternate file instead of go.mod
in the module root directory. The file’s name must end with .mod
. A file named go.mod
must still be present in order to determine the module root directory, but it is not accessed. When -modfile
is specified, an alternate go.sum
file is also used: its path is derived from the -modfile
flag by trimming the .mod
extension and appending .sum
.
По умолчанию, модули грузятся в кэш, но если мы хотим поработать со старыми пакетами, мы можем зафризить в папку vendor определенные версии пакетов:
Это создаст папку vendor и туда скопирует все пакеты для разработки и тестов. Так же будет создан файл vendor/modules.txt
, который будет содержать версии пакетов. Можем их посмотреть:
Эти команды сравнят версии в vendor с версиями в go.mod. Если они совпадут, вылетит ошибка. Надо будет ввести go mod vendor
снова.
Начиная с Go 1.14, поддержка vendor включена по умолчанию. Это значит, что go build
и go test
будут подгружать модули из vendor, а не идти в интернет или локальный кэш пакетов. go-команды игнорируют любые другие директории vendor, кроме той, что в корневом модуле.
Обновление модулей в go.mod.
Установка модулей
Загрузка модуля в кэш модулей
Редактирование и форматирование go.mod.
Граф зависимостей
Инициализация модуля в директории
Подтягивание в папку vendor зависимостей для сборки и теста проекта
Проверка, что все зависимости разрешены
-mod=mod
tells the go
command to ignore the vendor directory and to go.mod
, for example, when an imported package is not provided by any known module.
By default, if the in go.mod
is 1.14
or higher and a vendor
directory is present, the go
command acts as if -mod=vendor
were used. Otherwise, the go
command acts as if -mod=readonly
were used.
The -modcacherw
flag instructs the go
command to create new directories in the module cache with read-write permissions instead of making them read-only. When this flag is used consistently (typically by setting GOFLAGS=-modcacherw
in the environment or by running go env -w GOFLAGS=-modcacherw
), the module cache may be deleted with commands like rm -r
without changing permissions first. The command may be used to delete the module cache, whether or not -modcacherw
was used.