go - 为什么不能从绝对路径构建?

标签 go

出于某种原因,我想从源代码构建一个 go 项目(docker swarm),遵循 official doc .

如果我这样做效果很好:

...
cd $GOPATH/src/github.com/docker/swarm
go install .

但如果我尝试“单行”它并避免 cd:

go install $GOPATH/src/github.com/docker/swarm

ERROR: can't load package: 
package <my go path>/src/github.com/docker/swarm: 
import "<my go path>/src/github.com/docker/swarm": 
cannot import absolute path

为什么不能去处理这个绝对路径?

最佳答案

JimB 是正确的,包是相对于导入路径的。无法“绝对”导入。

虽然规范中没有具体说明,但它确实在 https://golang.org/ref/spec#ImportPath 处有所提及:

The interpretation of the ImportPath is implementation-dependent but it is typically a substring of the full file name of the compiled package and may be relative to a repository of installed packages.

相对导入和 vendor 有多种可能对您有用(请参阅 GO 1.5 vendor 实验,现在在 1.6 https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/edit?pref=2&pli=1 中可用)

关于go - 为什么不能从绝对路径构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218930/

相关文章:

algorithm - 使用 Golang 的遗传算法中的轮盘赌选择

python - 在 pyyaml 中处理 bool 值

go - 什么是二次过程?

go - 如何获取接口(interface)的指针

go - 将单独的值传递给模板函数

go - 如何从另一个 Go 应用程序运行 Go 编译器

go - 如何在 Go 中使用基类型设置结构字段

Golang 和 DDD 域建模

使用 Istio grpc-web 代理的 CORS 预检上的 golang gRPC 503

mysql - 最新的 Mysql 和 Postgres 会自动准备每个查询吗?