Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't go build from absolute path?

Tags:

go

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?

like image 830
PierreF Avatar asked May 13 '16 20:05

PierreF


2 Answers

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)

like image 170
Kevin Deenanauth Avatar answered Oct 12 '22 22:10

Kevin Deenanauth


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.

like image 33
Kareem Avatar answered Oct 12 '22 23:10

Kareem