Reputation: 2752
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?
Upvotes: 19
Views: 23871
Reputation: 571
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.
Upvotes: 5
Reputation: 956
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)
Upvotes: 7