Migrating from Dep to Go Modules: A Step-by-Step Guide
As you're planning to transition from Dep to Go modules, here's a comprehensive guide to effortlessly migrate your project:
-
Confirm Go Version Compatibility: Ensure you're running Go version 1.11 or later by executing go version.
-
Move Outside of GOPATH: Position your code outside of GOPATH or set export GO111MODULE=on.
-
Import Dependencies with go mod init: Run go mod init [module path] to transfer dependencies from Gopkg.lock.
-
Tidy Up with go mod tidy: This command eliminates redundant imports and includes indirect ones.
-
Delete Vendor Folder (Optional): Consider removing the vendor folder for obsolete Dep files by executing rm -rf vendor/ or moving it to the trash.
-
Test Build: Perform a test build using go build to verify functionality.
-
Remove Obsolete Files: Delete Gopkg.lock and Gopkg.toml files, as they're no longer needed in the Go module environment.
Go modules have successfully imported your dependencies from Dep using the Gopkg.lock file and generated a go.mod file, effectively managing your project's dependencies.
Preserving the Vendor Folder (Optional)
-
Copy Dependencies into Vendor: Run go mod vendor to transfer your dependencies to the vendor folder.
-
Build with Vendor: Use go build -mod=vendor to instruct Go build to utilize your vendor folder.
The above is the detailed content of How to Migrate from Dep to Go Modules: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!