Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 1.79 KB

modules.md

File metadata and controls

87 lines (60 loc) · 1.79 KB

Overview

  1. How modules work
  2. See also workspaces doc after

Purpose

  • Modules are how Go manages dependencies
  • Modules collect related packages
  • Modules expose packages to other modules

Create a module

go mod init company.com/foo;
go mod tidy;

Clear cache

cd $PROJ_ROOT;
rm -vf ./go.mod;
rm -vf ./go.sum;
go clean -modcache;

go mod init wcarmon.com/codegen;
go mod tidy;

Prune unused deps

cd /dir/with/go.mod/

go mod tidy;

Replacing repo with fork

go mod edit -replace github.com/dave/dst=github.com/hawkinsw/dst@generics

Using local package

  1. See: https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive
// in go.mod
replace example.com/original/import/path => /your/forked/import/path

replace example.com/original/import/path => ../relative/dir/to/another/go.mod/

Version control

  1. commit go.sum and go.mod files

Goland

  1. Goland will auto-import new dependencies only after you setup a module

Listing old dependencies

go list -u -m -f '{{if .Update}}{{.}}{{end}}' all
  1. See also: https://github.com/psampaz/go-mod-outdated
go list -u -m -json all | $HOME/go/bin/go-mod-outdated;

Visualizing dependencies

  1. https://blog.jetbrains.com/go/2020/03/16/working-with-go-modules-dependency-management/#visualizing-the-dependencies-of-a-go-project

Deprecated

  1. replace directives should be replaced with workspaces
  • TODO: mention package cycles

Other Resources

  1. https://go.dev/ref/mod
  2. https://go.dev/doc/tutorial/create-module
  3. https://go.dev/blog/using-go-modules
  4. https://www.practical-go-lessons.com/chap-17-go-modules
  5. https://www.practical-go-lessons.com/chap-41-cheatsheet#go-modules