How do I migrate from Dep to Go Modules
Migrating from Dep to Go Modules is very easy.
- Run
go version
and make sure you're using Go version 1.11 or later. - Move your code outside of GOPATH or set
export GO111MODULE=on
. go mod init [module path]
: This will import dependencies from Gopkg.lock.go mod tidy
: This will remove unnecessary imports, and add indirect ones.- (Optional) Delete your vendor folder (
rm -rf vendor/
or move to trash) go build
: Do a test build to see if it works.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:
- Run
go mod vendor
to copy your dependencies into the vendor folder. - Run
go build -mod=vendor
to ensurego build
uses your vendor folder.
To add to @Nicholas answer's:
Here is from the offical golang documenation:
To create a go.mod for an existing project:
- Navigate to the root of the module's source tree outside of GOPATH:
$ export GO111MODULE=on # manually active module mode
$ cd $GOPATH/src/<project path> # e.g., cd $GOPATH/src/you/hello
- Create the initial module definition and write it to the go.mod file:
$ go mod init
This step converts from any existing dep Gopkg.lock file or from any of the other nine total supported dependency formats, adding require statements to match the existing configuration.
- Build the module. When executed from the root directory of a module, the ./... pattern matches all the packages within the current module. go build will automatically add missing or unconverted dependencies as needed to satisfy imports for this particular build invocation:
$ go build ./...
- Test the module as configured to ensure that it works with the selected versions:
$ go test ./...
(Optional) Run the tests for your module plus the tests for all direct and indirect dependencies to check for incompatibilities:
$ go test all