How do I migrate from Dep to Go Modules
Asked Answered
E

3

46

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

How do I migrate?

Entropy answered 13/4, 2019 at 10:49 Comment(0)
E
82

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.
Entropy answered 13/4, 2019 at 10:49 Comment(5)
After I ran these when I try to build, I am getting this error clang: error: unable to read SDK settings for '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'Ringdove
@Ringdove try updating your CommandLineTools: xcode-select --installEntropy
Yes did that and did this too xcode-select --switch /Library/Developer/CommandLineTools/ Still no luck @NicholasRingdove
Be cautious with step 5. If your repo has vendor folder, it quite possibly contains some stuff unavailable otherwiseWedurn
Worked for me in 2023Apocrypha
R
5

To add to @Nicholas answer's:

Here is from the offical golang documenation:

To create a go.mod for an existing project:

  1. 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
  1. 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.

  1. 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 ./...
  1. Test the module as configured to ensure that it works with the selected versions:
$ go test ./...
  1. (Optional) Run the tests for your module plus the tests for all direct and indirect dependencies to check for incompatibilities:

$ go test all
Romonaromonda answered 14/4, 2019 at 8:0 Comment(0)
E
2

Another way to upgrade to modules.

  • Remove the Gopkg.toml and Gopkg.lock

    rm Gopkg.*

  • Initialise the Go modules

    GO111MODULE=on go mod init

  • Run go mod tidy to pull all the indirect modules and remove unused modules

    GO111MODULE=on go mod tidy

  • Run build to ensure everything works fine

    go build

Tip in case you face few modules not found then manually update the modules tag in go.mod file.

Epicurean answered 20/11, 2019 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.