Why can't you build from an absolute path?

For some reason, I want to build a go project (dockers) from a source, following an official document.

This works well if I:

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

But this fails if I try to "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 you deal with this absolute path?

+5
source share
1 answer

JimB is correct, packages belong to the import path. Unable to import absolutely.

Unless specified specifically in the specification, it refers to it https://golang.org/ref/spec#ImportPath :

The interpretation of ImportPath is implementation dependent, but this is usually a substring of the fully qualified file name of the compiled package and may refer to the repository of installed packages.

There are various options for relative imports and vending that may work for you (see the GO 1.5 test experiment, now available in version 1.6 https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo/edit?pre = 2 & pli = 1 )

+3
source

All Articles