why go get does not work outside a module?

1,650 views
Skip to first unread message

Manlio Perillo

unread,
Feb 15, 2019, 9:23:10 AM2/15/19
to golang-nuts
I have started to use go modules recently, and I have set GO111MODULE=on.

It was unexpected to found that go get does not work outside a module.
As an example:
go: cannot determine module path for source directory /home/manlio (outside GOPATH, no import comments)

When I used go get inside a module I found that:

1. It saves the downloaded packages in the go mod cache
2. It Installs the command in GOBIN

These two points can be also done outside a module.

3. Add an (indirect) entry in the require section in the go.mod file

The new entry will, however, be removed by go mod tidy.

Is it really necessary that go get must only work inside a module?


Thanks
Manlio Perillo

Tyler Compton

unread,
Feb 15, 2019, 7:08:08 PM2/15/19
to Manlio Perillo, golang-nuts
It sounds to me like you would want to set GO111MODULE to "auto". My understanding is that then "go get" will behave like it used to when run inside of the GOPATH or in a directory tree without a go.mod file.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manlio Perillo

unread,
Feb 16, 2019, 7:03:03 AM2/16/19
to golang-nuts
Well, the problem is that module aware go get will **still** download commands in GOBIN and **will** cache downloaded modules in
GOPATH/pkg/mod.

So I don't see a valid reason why it should not work outside a module.

Thanks
Manlio Perillo

thepud...@gmail.com

unread,
Feb 16, 2019, 8:00:50 AM2/16/19
to golang-nuts
The good news is that this is addressed for 1.12:

  #24250 cmd/go: allow "go get" when outside a module in module mode

It would be worthwhile to try the 1.12 release candidate if you can:

  
My understanding is that in the lead-up to 1.11, it was a bit debatable how it should work, so the thinking was that it would be better to first get some more experience as part of the initial modules experiment in 1.11.
  
You can do 'go get' for a binary outside of a module in 1.11, but it is not as natural as it is in Go 1.12.  There are a few options outlined here in this FAQ (along with some more background, a sample helper script, etc.):


That FAQ is a little broader than your specific question, but you can look over the 1.11 workarounds listed in that FAQ, which hopefully gives a more complete description than my quick comment above.

One option in a particular that is mentioned in that FAQ is https://github.com/myitcv/gobin, which is a community tool targeting this area of functionality that many people are using now. 'gobin' has a bit more flexibility, which you can read about at that repo.

Regards,
thepudds

Manlio Perillo

unread,
Feb 16, 2019, 11:30:00 AM2/16/19
to golang-nuts
Thanks.

The comment about tools dependencies was very interesting.

Other package managers, like pipenv for Python, have the concept of dev dependencies.
Basically, required packages are divided in two groups: packages and dev-packages:

Another interesting difference is that pipenv declares how to get packages in the Pipfile, but Go uses the GOPROXY environment variable.

The approach used by gobin is a bit too verbose for me:

  //go:generate gobin -m -run golang.org/x/tools/cmd/stringer -type=Pill

Manlio Perillo


On Saturday, February 16, 2019 at 2:00:50 PM UTC+1, thepud...@gmail.com wrote:
The good news is that this is addressed for 1.12:

  #24250 cmd/go: allow "go get" when outside a module in module mode

It would be worthwhile to try the 1.12 release candidate if you can:

  
My understanding is that in the lead-up to 1.11, it was a bit debatable how it should work, so the thinking was that it would be better to first get some more experience as part of the initial modules experiment in 1.11.
  
You can do 'go get' for a binary outside of a module in 1.11, but it is not as natural as it is in Go 1.12.  There are a few options outlined here in this FAQ (along with some more background, a sample helper script, etc.):


That FAQ is a little broader than your specific question, but you can look over the 1.11 workarounds listed in that FAQ, which hopefully gives a more complete description than my quick comment above.

One option in a particular that is mentioned in that FAQ is https://github.com/myitcv/gobin, which is a community tool targeting this area of functionality that many people are using now. 'gobin' has a bit more flexibility, which you can read about at that repo.

Regards,
thepudds

> [...] 
Reply all
Reply to author
Forward
0 new messages