Yeah, it's not going to be easy

Search

Search IconIcon to open search

go module

Oct 12, 2023

This post is a translation of the original Korean post.


module

How Go manages dependencies

# create go module

1
go mod init <module-path>

# go.mod file

The file where the module is defined

  • module Directive
    • The full name of the module
    • The path to the module (module-path)
      • Path that acts as a prefix for all packages within the module.
  • go Directive
    • Sets the minimum version of Go required to use this module
  • require directive
    • Declares the minimum required version of a module dependency
    • indirect annotation
      • Indirect dependencies not directly imported

# go commands

# go get

Update dependencies (update go.mod file)

  • Do not build or install packages (go version >= 1.18)

# go install

Build and install

  • Installation Directories
    • 1st: GOBIN
    • 2nd: GOPATH/bin (or $HOME/go/bin if no GOPATH)
    • executable in GOROOT
      • Installed in $GOROOT/bin or $GOTOOLDIR
  • non-executable packages
    • Built and cached, but not installed

# go build

compile packages and dependencies into executable files

  • Resulting file: first source file or source code directory name
  • -o option
    • Force the resulting file to be written to the specified output file or directory

# go mod download

Downloads module to module cache

# go mod tidy

Clean up packages, delete and add packages

  • Verify that the module’s source code and go.mod file match
  • Check which modules provide the package
    • Add missing modules, remove unneeded modules

# module cache

directory where modules downloaded with the go command are stored

  • Default location: $GOPATH/pkg/mod

# go command mode

  • GOPATH` mode
    • Ignore modules
    • Look for dependencies in vendor directories and GOPATH
  • module-aware mode (default: go version >= 1.16)
    • Use go.mod files to find dependencies
    • Load packages from module cache (download missing modules)

# GO111MODULE environment variable

  • off
    • GOPATH mode
  • on (or unset)
    • module-aware mode
  • auto
    • module-aware mode when a go.mod file exists in the current or parent directory
    • The go mod subcommand and the go install version query use module-aware mode even if no go.mod file exists.

# GOPROXY environment variable

Configure the go command to connect to a proxy or source control server

  • List of module proxy URLs
  • direct keyword
    • Indicates that it should communicate with a VCS (e.g. git)
  • off keyword
    • Indicates that no communication should be attempted

# -mod option

  • mod
    • ignore vendor directory
    • Automatically update go.mod and go.sum (e.g. if the imported package does not come from a known module)
      • go get and go mod tidy do this automatically
  • readonly (default)
    • Ignore the vendor directory
    • Report an error if go.mod needs to be updated
  • vendor
    • Use vendor directory
    • do not use network or module cache

# References