编辑
2020-02-18
编程语言
00
请注意,本文编写于 1322 天前,最后修改于 113 天前,其中某些信息可能已经过时。

目录

package
init函数
远程获取包
依赖管理

Golang的工程管理一直是初学者所不能很好的理解的事物,本篇主要讲述了在Golang中如何进行工程管理,如何构建可复用模块,从GitHub获取Go的包是如何操作的,以及Golang的包管理工具的基本使用,这样再进行项目开发的时候就可以模块化的管理自己的工程,首先还得从Package说起!

mark

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函数,这点比较特殊

mark

远程获取包

就拿这个concurrentMap来说吧,这是一个Go语言的concurrentMap实现, https://github.com/easierway/concurrent_map 现在假设我们需要引入这个包,(在配置好GOPATH的情况下)直接使用命令搞定:

mark

命令执行完毕就会在GOPATH的目录下直接把go源文件拉下来,接下来导入就好了

mark

所以我们在提交自己的开源项目的时候,别把src目录放进去,只要把代码的路径放在相对根目录下就好了,比如:mark

依赖管理

Go未解决的依赖问题: 1、同一环境下,不同项目使用同一包的不同版本 2、无法管理对包的特定版本的依赖

vender路径

随着Go 1.5 release版本的发布,vendor目录被添加到除了GOPATH和GOROOT之外的依赖目录查找的解决方案。在Go1.6之前,你需要手动的设置环境变量

查找依赖包路径的解决方案如下: 1、当前包下的vendor目录 2、向上级目录查找,直到找到src下的vendor目录 3、在GOPATH下面查找依赖包 4、在GOROOT目录下查找

常用的依赖管理工具:

mark

接下来就简单演示一下glide在Windows下的使用:

1、首先安装glide,推荐直接通过go的源码安装

进入GOPATH路径:比如我的GOPATH路径是D:\mycode\practic_code\go_learning\

bash
D:\mycode\practic_code\go_learning>go get -u github.com/Masterminds/glide

编译glide

bash
D:\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

mark

好了,接下来重新编译出glide.exe,如果没在bin中,把它放到bin目录就好了

2、glide init命令

假设我的一个工程依赖与某个远程package

mark

bash
D:\mycode\practic_code\go_learning\src\ch15\remote>glide 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 test reference to github.com\easierway\concurrent_map [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 0.9.1. 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 0.9.1 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 '>= 0.9.1, < 1.0.0' ('^0.9.1') [INFO] - Tracking patch version releases would use '>= 0.9.1, < 0.10.0' ('~0.9.1') [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) Y [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/ D:\mycode\practic_code\go_learning\src\ch15\remote>

提示的意思其实很清楚,按照它的提示一步一步来就好了,会在目录下生成glide.yaml,就和Maven一样的:

yaml
package: ch15/remote import: [] testImport: - package: github.com/easierway/concurrent_map version: 0.9.1

3、glide install命令

执行完毕后,发现依赖被弄到了vender文件夹底下,所以与其说是导包,不如说是直接把依赖的go文件下载下来了,放到vender目录下,这就是glide的管理方式:

mark

本文作者:Tim

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!