Check the latest Golang edition
If you ask me , Why use the latest ? Because we are all software lovers , We should be eager to test the latest technology ! ( By the way , You can use it. Golang 1.11.X, But you should want to know why not use the latest available version …… )
Find the project to migrate
In this paper , I will migrate a personal project that I worked on a few months ago . In this project , I use Glide To manage dependencies . You can do the same thing for any of your projects .
Don't worry about mollydb What did you do , Because we only need to understand how to migrate existing projects .
git clone https://github.com/wesovilabs/mollydb.git
cd mollydb
git checkout -b feature/using-go-mods
Adjust the project structure
This project uses a src Folder built , This folder contains a subfolder mollydb, In this subfolder, there is a vendor Directories are used to store dependencies .
src > mollydb > vendors
Delete vendor Catalog
rm -rf src/mollydb/vendor
Run the following command to initialize go modeule and Pull to rely on
go mod init mollydb # initialization go module
go mod tidy # Pull to rely on
After the command is executed , You will get a go.mod and go.sum file , among go.mod The contents of the document will be as follows
module mollydb
require (
gitHub.com/boltdb/bolt v0.0.0 – 20180302180052-fd01fc79c553
gitHub.com/fsnotify/fsnotify v1.4.7
gitHub.com/go-yaml/yaml v0.0.0 – 20140922213225-bec87e4332ae
gitHub.com/graphql-go/graphql v0.0.0 – 20180324214652 – 8ab5400ff77c
gitHub.com/graphql-go/handler v0.0.0 – 20180312211735-df717460db9a
gitHub.com/graphql-go/relay v0.0.0 – 20171208134043 – 54350098cfe5
golang.org/x/net v0.0.0 – 20180320002117 – 6078986fec03
golang.org/x/sys v0.0.0 – 20180318190847 – 01acb38716e0
gopkg.in/yaml.v2 v2.1.1
)
Run the program
We just need to run the following command to verify whether the project works as before .
go run main.go
All succeeded
go module vs vendor
go1.5 introduce vendor To manage module dependencies ,go1.11 introduce module Concept to replace vendor, Become the latest recommended package management tool . Detailed comparison can be read :Go: Package management tools GOPATH、vendor、dep 、go module
go module and vendor There are two conflicting designs , There is only one choice between the two , Don't mix .
vendor Depends on GOPATH, Then you have to set up GOPATH
module Don't use GOPATH, Then you can not set GOPATH, Become an option .
from : Migrate to mod just 3 A step , The original text has been deleted .
In addition, it also refers to :go vendor Usage of 、Go: Package management tools GOPATH、vendor、dep 、go module