Golang依赖管理

创建  go.mod

        命令行创建(也可以直接IDE创建GO  modules  项目)

cd $目录
go mod int

        会在目录下生成  go.mod  文件

module learning  // 这里必须要和目录名是一致的

go 1.15

安装依赖

        如安装web框架  gin,从github  上获取地址

go get -u github.com/gin-gonic/gin

        go.mod  会自动更新

module learning

go 1.15

require (
	github.com/gin-gonic/gin v1.6.3 // indirect
	github.com/go-playground/validator/v10 v10.4.0 // indirect
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/json-iterator/go v1.1.10 // indirect
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
	github.com/modern-go/reflect2 v1.0.1 // indirect
	github.com/ugorji/go v1.1.10 // indirect
	golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
	golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
	google.golang.org/protobuf v1.25.0 // indirect
	gopkg.in/yaml.v2 v2.3.0 // indirect
)
更换版本
go get -u github.com/gin-gonic/gin@v1.5.0

        同样,go.mod  自动更新

module learning

go 1.15

require (
	github.com/gin-gonic/gin v1.5.0 // indirect
	github.com/go-playground/validator/v10 v10.4.0 // indirect
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/json-iterator/go v1.1.10 // indirect
	github.com/mattn/go-isatty v0.0.12 // indirect
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
	github.com/modern-go/reflect2 v1.0.1 // indirect
	github.com/ugorji/go v1.1.10 // indirect
	golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
	golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
	google.golang.org/protobuf v1.25.0 // indirect
	gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
	gopkg.in/yaml.v2 v2.3.0 // indirect
)

可能会出现多余的依赖,使用命令清除

go mod tidy
老项目更换成  go  mod

        在建立了go.mod文件后,执行

go build ./...