duanli12176
2016-05-13 20:34 阅读 103
已采纳

为什么不能从绝对路径进行构建?

For some reason, I want to build a go project (docker swarm) from source, following the official doc.

It works well if I do:

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

But it fails if I try to "one-line" it and avoid 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

Why can't go deal with this absolute path?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongya4335 dongya4335 2016-05-13 21:16

    JimB is correct, packages are relative to the import path. There is no capability to import 'absolutely'.

    While it is not spelled out specifically in the spec, it does allude to it at 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.

    There are variations on relative importing and vendoring that might work for you (see GO 1.5 vendoring experiment, now available in 1.6 https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/edit?pref=2&pli=1)

    点赞 评论 复制链接分享
  • douzheyo2617 douzheyo2617 2018-05-27 15:14

    I came here to find an answer to the same question, as I was doing the same thing and found there are two ways to do this...

    so I thought I would share:

    Run from within the package directory:

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

    and as a relative repo:

    go install github.com/docker/swarm
    

    There are some details in the official go docs here.

    点赞 评论 复制链接分享

相关推荐