querying whether the go command would run in module mode

53 views
Skip to first unread message

Dan Kortschak

unread,
Dec 8, 2019, 7:25:28 PM12/8/19
to golang-nuts
Is there a way to query whether an invocation of the go command would
be running in module mode?

thanks
Dan

Paul Jolly

unread,
Dec 9, 2019, 1:52:43 AM12/9/19
to Dan Kortschak, golang-nuts
go env GOMOD - gives the path to the go.mod in use in module mode, and is empty otherwise (i.e. GOPATH mode)

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c0c406db898a66fb1fe2638242922512315ff190.camel%40kortschak.io.

Dan Kortschak

unread,
Dec 9, 2019, 4:28:24 AM12/9/19
to Paul Jolly, golang-nuts
Thanks.

When you're not in a module it returns /dev/null on linux. I don't
suppose this is platform independent?

Jamil Djadala

unread,
Dec 9, 2019, 4:41:51 AM12/9/19
to golan...@googlegroups.com
On Mon, 09 Dec 2019 19:57:48 +1030
Dan Kortschak <d...@kortschak.io> wrote:

> Thanks.
>
> When you're not in a module it returns /dev/null on linux. I don't
> suppose this is platform independent?

Hi,
This command:
go env GOMOD

returns 1 emty line,
i dont see any /dev/null ?

--
Jamil Djadala

Paul Jolly

unread,
Dec 9, 2019, 5:04:51 AM12/9/19
to Dan Kortschak, golang-nuts
> When you're not in a module it returns /dev/null on linux. I don't
> suppose this is platform independent?

I have to say what you saw surprised me until Daniel Martí and I did a
bit of experimentation.

It seems the value of GO111MODULE matters here. If you have
GO111MODULE=on then the result of go mod env outside of a module
context is /dev/null (or your system's equivalent)

If you have GO111MODULE unset (i.e. auto) then it is empty string.

This is certainly not clear to me from the docs in go help environment.

I realise that this way (i.e. with the /dev/null output when
GO111MODULE=on) you can distinguish between modules being on/off, as
well as whether you're in a module context or not (regardless of
whether you have modules on/auto).

But this doesn't exactly jump out at you from the docs of go help
environment, and I for one have incorrectly advised people on how to
check "are you in a module context or not". Because the logic becomes
something like:

if gomod != "" && gomod != os.DevNull {
// we are in a module context

I'm going to raise an issue to suggest that the docs are clarified here.

Dan Kortschak

unread,
Dec 9, 2019, 5:29:38 AM12/9/19
to Paul Jolly, golang-nuts
Thanks.

Yes, I always have GO111MODULE=on, hence the difference.

Paul Jolly

unread,
Dec 9, 2019, 5:37:29 AM12/9/19
to Dan Kortschak, golang-nuts
I raised https://github.com/golang/go/issues/36052 for discussion.
Reply all
Reply to author
Forward
0 new messages