Files
the-way-to-go_ZH_CN/eBook/09.7.md
Haigang Zhou 471d59af32 第九章修改 (#841)
Co-authored-by: Joe Chen <jc@unknwon.io>
2022-05-11 02:04:17 +08:00

44 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 9.7 使用 go install 安装自定义包
go install 是 Go 中自动包安装工具:如需要将包安装到本地它会从远端仓库下载包:检出、编译和安装一气呵成。
在包安装前的先决条件是要自动处理包自身依赖关系的安装。被依赖的包也会安装到子目录下,但是没有文档和示例:可以到网上浏览。
go install 使用了 GOPATH 变量(详见[第 2.2 节](02.2.md))。
远端包(详见[第 9.5 节](09.5.md)
假设我们要安装一个有趣的包 `tideland`(它包含了许多帮助示例,参见[项目主页](http://code.google.com/p/tideland-cgl))。
因为我们需要创建目录在 Go 安装目录下,所以我们需要使用 root 或者 su 的身份执行命令。
确保 Go 环境变量已经设置在 root 用户下的 `./bashrc` 文件中。
使用命令安装:`go install tideland-cgl.googlecode.com/hg`
可执行文件 `hg.a` 将被放到 `$GOROOT/pkg/linux_amd64/tideland-cgl.googlecode.com` 目录下,源码文件被放置在 `$GOROOT/src/tideland-cgl.googlecode.com/hg` 目录下,同样有个 `hg.a` 放置在 `_obj` 的子目录下。
现在就可以在 go 代码中使用这个包中的功能了,例如使用包名 cgl 导入:
```go
import cgl "tideland-cgl.googlecode.com/hg"
```
从 Go1 起 go install 安装 Google Code 的导入路径形式是:`"code.google.com/p/tideland-cgl"`
升级到新的版本:
更新到新版本的 Go 之后本地安装包的二进制文件将全被删除。如果你想更新,重编译、重安装所有的 go 安装包可以使用:`go install -a`
go 的版本发布的很频繁所以需要注意发布版本和包的兼容性。go1 之后都是自己编译自己了。
go install 同样可以使用 go install 编译链接并安装本地自己的包(详见[第 9.8.2 节](09.8.md))。
更多信息可以在 [官方网站](http://golang.org/cmd/go/) 找到。
## 链接
- [目录](directory.md)
- 上一节:[为自定义包使用 godoc](09.6.md)
- 下一节:[自定义包的目录结构、go install 和 go test](09.8.md)