Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I migrate from Dep to Go Modules

I'm currently using Dep and would like to start using Go modules.

How do I migrate?

like image 844
Nic Avatar asked Apr 13 '19 10:04

Nic


People also ask

Where does go get put modules?

A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.


1 Answers

Migrating from Dep to Go Modules is very easy.

  1. Run go version and make sure you're using Go version 1.11 or later.
  2. Move your code outside of GOPATH or set export GO111MODULE=on.
  3. go mod init [module path]: This will import dependencies from Gopkg.lock.
  4. go mod tidy: This will remove unnecessary imports, and add indirect ones.
  5. (Optional) Delete your vendor folder (rm -rf vendor/ or move to trash)
  6. go build: Do a test build to see if it works.
  7. rm -f Gopkg.lock Gopkg.toml: Delete the obsolete files used for Dep.

Go has imported my dependencies from Dep by reading the Gopkg.lock file and also created a go.mod file.

If you want to keep your vendor folder:

  1. Run go mod vendor to copy your dependencies into the vendor folder.
  2. Run go build -mod=vendor to ensure go build uses your vendor folder.
like image 58
Nic Avatar answered Sep 24 '22 09:09

Nic