Is there a formal document explain the package search orders in Golang?

863 views
Skip to first unread message

gd

unread,
Dec 2, 2016, 2:24:19 PM12/2/16
to golang-nuts
It looks, for the official compiler, if the main package is under GOPATH, then the order is:
1. the vendor folder
2. the standard packages
3. GOPATH folder

If the main package is outsides of GOPATH, then the order is:
1. the standard packages
2. GOPATH folder
the local vendor folder is totally ignored.

I don't know if this is the well defined behaviour or not.

Ian Lance Taylor

unread,
Dec 2, 2016, 4:31:57 PM12/2/16
to gd, golang-nuts
You can see the defined behavior by running `go help gopath`. That
will tell you the search path when using the go tool.

If you are not using the go tool, the behavior is different.

Ian

di...@veryhaha.com

unread,
Dec 3, 2016, 3:16:21 AM12/3/16
to golang-nuts, di...@veryhaha.com


looks `go help gopath` doesn't mention standard packages are prior than GOPATH packages.
 

Ian

Peter Mogensen

unread,
Dec 3, 2016, 5:25:34 AM12/3/16
to golan...@googlegroups.com


On 2016-12-02 22:31, Ian Lance Taylor wrote:
> On Fri, Dec 2, 2016 at 11:24 AM, gd <di...@veryhaha.com> wrote:
>> It looks, for the official compiler, if the main package is under GOPATH,
>> then the order is:
>> 1. the vendor folder
>> 2. the standard packages
>> 3. GOPATH folder
>>
>> If the main package is outsides of GOPATH, then the order is:
>> 1. the standard packages
>> 2. GOPATH folder
>> the local vendor folder is totally ignored.
>>
>> I don't know if this is the well defined behaviour or not.
>
> You can see the defined behavior by running `go help gopath`. That
> will tell you the search path when using the go tool.

Maybe I read the help text wrong, but it doesn't really answer the above
question does it?

In general it seems using "go build" in the project dir ignores the
vendor folder if the project dir is not under "src" in the GOPATH. Even
if there's code in the project dir and a vendor folder.

So... assuming this (and GOPATH=$PWD):

$ tree
.
├── main.go
├── src
│ └── myapp -> ..
└── vendor
├── github.com
│ └── pkg
│ └── errors
│ ├── appveyor.yml
│ ├── bench_test.go
│ ├── errors.go
│ ├── errors_test.go
│ ├── example_test.go
│ ├── format_test.go
│ ├── LICENSE
│ ├── README.md
│ ├── stack.go
│ └── stack_test.go
└── manifest

Executing "go build" in the top directory ignores the vendor folder.
But exexuting "go build myapp" works and includes the vendor folder.

/Peter

di...@veryhaha.com

unread,
Dec 3, 2016, 10:14:50 AM12/3/16
to golang-nuts, a...@one.com

You mean myapp can import the packages in the vendor folder?
I tested it, but without success.
 

Peter Mogensen

unread,
Dec 3, 2016, 10:42:11 AM12/3/16
to golan...@googlegroups.com


On 2016-12-03 16:14, di...@veryhaha.com wrote:
> You mean myapp can import the packages in the vendor folder?
> I tested it, but without success.

Yes.

If you use:
$ go build myapp

It works. The vendor folder will be used. Not with plain "go build" though.

Some tools (like go guru) hasn't got a way to name the current package
though, so for guru to work, you need to:
$ (cd src/myapp; guru ....)

/Peter

Dave Cheney

unread,
Dec 3, 2016, 7:49:48 PM12/3/16
to golang-nuts, a...@one.com
It looks like your source does not match the GOPATH format. If this is the case then the go tool will not work reliably.

Try putting the source of your program in 


and the vendored packages in


You _can_ elide the github.com/yourname part if you don't want to share this project and make it go gettable by others, but please remember that you cannot have source at the top level of $GOPATH/src, as they will not have a valid package name.
Reply all
Reply to author
Forward
0 new messages