简介
老大说来一个包管理工具,我的苦逼之路开始了!
坑1:新手没有使用依赖代理导致有的包无法下载
坑2:依赖本地包
坑3:已有项目使用 go mod
本文简单使用了 go mod,并过了这3个坑
前提
go version >= 1.11.x
配置依赖代理
goland Enable Go Modules(vgo)
简单流程
- go mod init [mod 名称]
- go mod tidy
- 完事(修改生成的 go.mod 会自动依赖)
注意事项
依赖本地项目
module go-price-service
require (
github.com/forestgiant/sliceutil v0.0.0-20160425183142-94783f95db6c
github.com/fsnotify/fsnotify v1.4.7
github.com/gin-gonic/gin v1.4.0
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/jinzhu/gorm v1.9.10
github.com/leodido/go-urn v1.1.0 // indirect
github.com/lexkong/log v0.0.0-20180607165131-972f9cd951fc
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.4.0
go-base v0.0.0
gopkg.in/go-playground/validator.v9 v9.29.0
)
replace go-base => ../go-base
其中 go-base 就是我的一个本地 go mod 项目
如何使用该项目?
- 首先 require 该项目版本指定为 V0.0.0(为什么参考连接)
- 通过 replace 替换,将依赖替换到本地路径
已有项目使用 go mod
使用 go mod 导致导包出现问题
比如我的一个项目 go-xxx 路径为 ../go/src/xxx.com/go-xxx
使用 go mod init go-xxx
那么项目内导包路径应该从 xxx.com/go-xxx/xxx 修改为 go-xxx/xxx