go mod
golang1.11版本添加了go mod来管理项目引用的第三方的包,并且可以和vendor互相切换。
保证go version在1.11以上。你的项目是不能直接在$GOPATH/src
下的,但是可以在$GOPATH/src
下再建立一个文件来作为项目根目录,或者在其他非$GOPATH/src
的目录建立项目。我选择在$GOPATH/src/p1-dhh
来作为项目根目录。
想要使用先学习一下go mod的用法。
- go mod启用,项目目录非
$GOPATH/src
,用 export GO111MODULE=on|off|auto来控制go mod的开启。
on:开启。off:关闭。auto:自动(如果是在$GOPATH/src
下,那么直接用$GOPATH/src
) - go mod命令行参数:
go mod <command> [arguments] | 含义 |
---|---|
download | download modules to local cache(下载模块到本地cache目录下) |
edit | edit go.mod from tools or scripts(编辑go.mod文件) |
graph | print module requirement graph(打印出模块的需求图) |
init | initialize new module in current directory(初始化当前目录的新模块) |
tidy | add missing and remove unused modules(添加缺失的、移除废弃的模块) |
vendor | make vendored copy of dependencies(将依赖模块的首层拷贝到当前目录下vendor文件夹) |
verify | verify dependencies have expected content(验证模块依赖) |
why | explain why packages or modules are needed (解释包或者模块为啥被依赖) |
- 使用go mod:初始化 go mod init;添加依赖 go mod tidy
- go mod 替换golang.org/x
module p1-dhh
go 1.12
replace (
golang.org/x/net => github.com/golang/net latest
golang.org/x/sync => github.com/golang/sync latest
golang.org/x/sys => github.com/golang/sys latest
)
golang.org/x的包都有对应的github的镜像,不知道版本不要紧,统一用latest最新,然后命令行 go mod tidy,就会更新文件了。
vendor
地址:github.com/kardianos/govendor
安装好以后,试试命令行govendor,如果找不到命令,那就把govendor的可执行文件放到该放的地方,或者配置好环境变量。
- govendor命令参数:
govendor <command> | 含义 |
---|---|
init | Create the “vendor” folder and the “vendor.json” file.(创建vendor/vendor.json到当前目录) |
list | List and filter existing dependencies and packages.(列出依赖包) |
add | Add packages from $GOPATH.(从$GOPATH添加依赖包) |
update | Update packages from $GOPATH. (更新依赖包) |
remove | Remove packages from the vendor folder.(移除依赖包) |
status | Lists any packages missing, out-of-date, or modified locally.(列出任何丢失、过期或本地修改的包) |
fetch | Add new or update vendor folder packages from remote repository.(从远程存储库添加新的或更新vendor文件的包) |
sync | Pull packages into vendor folder from remote repository with revisions from vendor.json file.(将包从远程存储库拖到vendor文件夹,并修改json文件) |
migrate | Move packages from a legacy tool to the vendor folder with metadata.(将包从遗留工具移动到包含元数据的vendor文件夹) |
get | Like “go get” but copies dependencies into a “vendor” folder. |
license | List discovered licenses for the given status or import paths. |
shell | Run a “shell” to make multiple sub-commands more efficient for large projects. |
- govendor 补充参数:
+local | (l) packages in your project |
+external | (e) referenced packages in GOPATH but not in current project |
+vendor | (v) packages in the vendor folder |
+std | (s) packages in the standard library |
+excluded | (x) external packages explicitly excluded from vendoring |
+unused | (u) packages in the vendor folder, but unused |
+missing | (m) referenced packages but not found |
+program | § package is a main package |
+outside | +external +missing |
+all | +all packages |
- 初始化 govendor init
- 添加包 govendor add xxxx
之后如果有新的体会会继续更新。