go语言学习笔记——包管理工具

138 阅读2分钟

Go未解决的问题

1、同一个环境下,不同项目,只用同一个依赖包的不同版本

2、无法管理对包的特定版本的依赖

解决办法:

在1.5版本之后,在项目下可以添加一个vender目录,查找的顺序

1、在当前包的vender下查找对应的依赖

2、想上级目录查找,直到找到src目录下的vender目录

3、在GOPATH下查找依赖包

4、在GOROOT下查找依赖包

常用的依赖管理工具:godep、glide、dep

安装glide(windows)

使用go get 命令安装glide

$ go get github.com/Masterminds/glide

这个时候开始进行安装,如果无法下载,记得设置下环境变量

GOPROXY=https://goproxy.io

安装之后,glide.exe会被安装在GOPATH的bin文件夹下面

我的环境变量GOPATH=D:\Go; 所以会被安装在D:\Go\bin下面

初始化项目

将路径切换到项目根目录,进行初始化

$ glide.exe init
[INFO]  Generating a YAML configuration file and guessing the dependencies
[INFO]  Attempting to import from other package managers (use --skip-import to skip)
[INFO]  Scanning code to look for dependencies
[INFO]  --> Found reference to errors
[INFO]  --> Found reference to fmt
[INFO]  --> Found reference to sync
[INFO]  --> Found reference to unsafe
[INFO]  --> Found test reference to Class21
[INFO]  --> Found test reference to github.com\easierway\concurrent_map
[INFO]  --> Found test reference to math\rand
[INFO]  --> Found test reference to os
[INFO]  --> Found test reference to strconv
[INFO]  --> Found test reference to testing
[INFO]  --> Found test reference to time
[INFO]  Writing configuration file (glide.yaml)
[INFO]  Would you like Glide to help you find ways to improve your glide.yaml configuration?
[INFO]  If you want to revisit this step you can use the config-wizard command at any time.
[INFO]  Yes (Y) or No (N)?
Y
[INFO]  Looking for dependencies to make suggestions on
[INFO]  --> Scanning for dependencies not using version ranges
[INFO]  --> Scanning for dependencies using commit ids
[INFO]  Gathering information on each dependency
[INFO]  --> This may take a moment. Especially on a codebase with many dependencies
[INFO]  --> Gathering release information for dependencies
[INFO]  --> Looking for dependency imports where versions are commit ids
[INFO]  Here are some suggestions...
[INFO]  The package github.com/easierway/concurrent_map appears to have Semantic Version releases (http://semver.org).
[INFO]  The latest release is v1.0.0. You are currently not using a release. Would you like
[INFO]  to use this release? Yes (Y) or No (N)
y
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com/easierway/concurrent_map to use the release v1.0.0 instead of no release
[INFO]  The package github.com/easierway/concurrent_map appears to use semantic versions (http://semver.org).
[INFO]  Would you like to track the latest minor or patch releases (major.minor.patch)?
[INFO]  The choices are:
[INFO]   - Tracking minor version releases would use '>= 1.0.0, < 2.0.0' ('^1.0.0')
[INFO]   - Tracking patch version releases would use '>= 1.0.0, < 1.1.0' ('~1.0.0')
[INFO]   - Skip using ranges
[INFO]  For more information on Glide versions and ranges see https://glide.sh/docs/versions
[INFO]  Minor (M), Patch (P), or Skip Ranges (S)?
S
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
N
[INFO]  Configuration changes have been made. Would you like to write these
[INFO]  changes to your configuration file? Yes (Y) or No (N)
Y
[INFO]  Writing updates to configuration file (glide.yaml)
[INFO]  You can now edit the glide.yaml file.:
[INFO]  --> For more information on versions and ranges see https://glide.sh/docs/versions/
[INFO]  --> For details on additional metadata see https://glide.sh/docs/glide.yaml/

初始化之后,会生成一个yml的文件,里面有对应的依赖的信息以及对应的版本