构建可复用模块
Golang的工程管理一直是初学者所不能很好的理解的事物,本篇主要讲述了在Golang中如何进行工程管理,如何构建可复用模块,从GitHub获取Go的包是如何操作的,以及Golang的包管理工具的基本使用,这样再进行项目开发的时候就可以模块化的管理自己的工程,首先还得从Package说起!
package
1、基本复用模块单元,以首字母大写来表明可被包外代码访问 2、代码的package可以和所在的目录不一致 3、同一目录里的Go代码的package要保持一致 4、通过go get来获取远程依赖
- go get -u 强制从网络更新远程依赖
5、注意代码在Github上的组织形式,以适应go get
- 直接以代码路径开始,不要有src
注意GOPATH的配置
init函数
1、在main被执行前,所有依赖的package的init方法都会被执行 2、不同包的init函数按照包导入的依赖关系决定执行顺序 3、每个包可以有多个init 函数 4、包的每个源文件也可以有多个init函数,这点比较特殊
远程获取包
就拿这个concurrentMap来说吧,这是一个Go语言的concurrentMap实现, https://github.com/easierway/concurrent_map 现在假设我们需要引入这个包,(在配置好GOPATH的情况下)直接使用命令搞定:
命令执行完毕就会在GOPATH的目录下直接把go源文件拉下来,接下来导入就好了
所以我们在提交自己的开源项目的时候,别把src目录放进去,只要把代码的路径放在相对根目录下就好了,比如:
依赖管理
Go未解决的依赖问题: 1、同一环境下,不同项目使用同一包的不同版本 2、无法管理对包的特定版本的依赖
vender路径
随着Go 1.5 release版本的发布,vendor目录被添加到除了GOPATH和GOROOT之外的依赖目录查找的解决方案。在Go1.6之前,你需要手动的设置环境变量
查找依赖包路径的解决方案如下: 1、当前包下的vendor目录 2、向上级目录查找,直到找到src下的vendor目录 3、在GOPATH下面查找依赖包 4、在GOROOT目录下查找
常用的依赖管理工具:
接下来就简单演示一下glide在Windows下的使用:
1、首先安装glide,推荐直接通过go的源码安装
进入GOPATH路径:比如我的GOPATH路径是D:\mycode\practic_code\go_learning\
1D:\mycode\practic_code\go_learning>go get -u github.com/Masterminds/glide
编译glide
1D:\mycode\practic_code\go_learning\src\github.com\Masterminds\glide>go build glide.go
如何配置正确glide.exe应该是在bin目录中,但是执行glide install 命令的时候有错误,需要更改
github.com\Masterminds\glide\path\winbug.go
好了,接下来重新编译出glide.exe,如果没在bin中,把它放到bin目录就好了
2、glide init命令
假设我的一个工程依赖与某个远程package
1D:\mycode\practic_code\go_learning\src\ch15\remote>glide init
2[INFO] Generating a YAML configuration file and guessing the dependencies
3[INFO] Attempting to import from other package managers (use --skip-import to skip)
4[INFO] Scanning code to look for dependencies
5[INFO] --> Found test reference to github.com\easierway\concurrent_map
6[INFO] Writing configuration file (glide.yaml)
7[INFO] Would you like Glide to help you find ways to improve your glide.yaml configuration?
8[INFO] If you want to revisit this step you can use the config-wizard command at any time.
9[INFO] Yes (Y) or No (N)?
10Y
11[INFO] Looking for dependencies to make suggestions on
12[INFO] --> Scanning for dependencies not using version ranges
13[INFO] --> Scanning for dependencies using commit ids
14[INFO] Gathering information on each dependency
15[INFO] --> This may take a moment. Especially on a codebase with many dependencies
16[INFO] --> Gathering release information for dependencies
17[INFO] --> Looking for dependency imports where versions are commit ids
18[INFO] Here are some suggestions...
19[INFO] The package github.com/easierway/concurrent_map appears to have Semantic Version releases (http://semver.org).
20[INFO] The latest release is 0.9.1. You are currently not using a release. Would you like
21[INFO] to use this release? Yes (Y) or No (N)
22Y
23[INFO] Would you like to remember the previous decision and apply it to future
24[INFO] dependencies? Yes (Y) or No (N)
25Y
26[INFO] Updating github.com/easierway/concurrent_map to use the release 0.9.1 instead of no release
27[INFO] The package github.com/easierway/concurrent_map appears to use semantic versions (http://semver.org).
28[INFO] Would you like to track the latest minor or patch releases (major.minor.patch)?
29[INFO] The choices are:
30[INFO] - Tracking minor version releases would use '>= 0.9.1, < 1.0.0' ('^0.9.1')
31[INFO] - Tracking patch version releases would use '>= 0.9.1, < 0.10.0' ('~0.9.1')
32[INFO] - Skip using ranges
33[INFO] For more information on Glide versions and ranges see https://glide.sh/docs/versions
34[INFO] Minor (M), Patch (P), or Skip Ranges (S)?
35S
36[INFO] Would you like to remember the previous decision and apply it to future
37[INFO] dependencies? Yes (Y) or No (N)
38Y
39[INFO] Configuration changes have been made. Would you like to write these
40[INFO] changes to your configuration file? Yes (Y) or No (N)
41Y
42[INFO] Writing updates to configuration file (glide.yaml)
43[INFO] You can now edit the glide.yaml file.:
44[INFO] --> For more information on versions and ranges see https://glide.sh/docs/versions/
45[INFO] --> For details on additional metadata see https://glide.sh/docs/glide.yaml/
46
47D:\mycode\practic_code\go_learning\src\ch15\remote>
提示的意思其实很清楚,按照它的提示一步一步来就好了,会在目录下生成glide.yaml,就和Maven一样的:
1package: ch15/remote
2import: []
3testImport:
4- package: github.com/easierway/concurrent_map
5 version: 0.9.1
3、glide install命令
执行完毕后,发现依赖被弄到了vender文件夹底下,所以与其说是导包,不如说是直接把依赖的go文件下载下来了,放到vender目录下,这就是glide的管理方式: