出于某种原因,我想从源代码构建一个 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/