go: question: "go: cannot find main module; see 'go help modules'"

What did you do?

If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best.

creating a file hello.go in /home/ubuntu/go/src/hello/

package main

import “fmt”

func main() { fmt.Printf(“hello, world\n”) }

What did you expect to see?

go into /home/ubuntu/go/src/hello/ call go build now i expected an created file hello (in go on windows it was hello.exe)

What did you see instead?

go: cannot find main module; see ‘go help modules’

Does this issue reproduce with the latest release (go1.12.5)?

every time (but only under ubuntu@DESKTOP windows sub system. After installing go on windows directly, all was working fine. I was able to call go build and an hello.exe was created. After calling hello.exe an hello was printed out on cmd. But now under ubunto on wsl i can call go version, go get, go list… but go build throw this “go: cannot find main module; see ‘go help modules’” error. I try to delete export GO111MODULE=on in .profile, but this issue is still here.

System details

go version go1.12.2 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ubuntu/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ubuntu/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOROOT/bin/go version: go version go1.12.2 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.2
uname -sr: Linux 4.4.0-17763-Microsoft
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.27-3ubuntu1) stable release version 2.27.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

After upgrade to go1.16, try to set :

go env -w GO111MODULE=auto

https://blog.golang.org/go116-module-changes

Thx

I try to delete export GO111MODULE=on in .profile, but this issue is still here.

You probably still have GO111MODULE=on set in your environment. If you’re using a sh-like shell, unset GO111MODULE or export GO111MODULE=auto should put things back into auto mode.

Hello @marcelotoledo, unlike other projects, we do not use the issue tracker for questions such as these. It is only used for bugs and feature proposals. Please feel free to ask it in any of these forums below:

Thanks

After upgrade to go1.16, try to set :

go env -w GO111MODULE=auto

https://blog.golang.org/go116-module-changes

Thx

work for me thanks !!!

This issue is closed, but it looks like a lot of people are seeing similar error messages since module-aware mode is now enabled by default in Go 1.16 (#41330).

I’ve opened #44745 to clarify the error messages. That should explain the problem and suggest a solution when possible. I’d like to backport that to the next 1.16 patch release.

Note that go env -w GO111MODULE=auto probably won’t work in Go 1.17. We plan to drop support for GOPATH builds and that environment variable.

Unsupported

Open a directory that contains a module in a subdirectory. gopls will not work in this case.

For vscode I have a workaround, do not know if this work for other editors.

My directory structure is:

Projects
  Someproject1
    microservice1
      go.mod
      go.sum
      main.go
    microservice2
      go.mod
      go.sum
      main.go
  Someproject2
  ...

opening “Somepoject1” in vscode causes a problem. To solve this I have made a second directory structure:

Workspaces
  Someproject1
  Someproject2

If i start working on a project I open the respective directory in "Workspace"s in vscode. There I add al the microservice directories from the corresponding project directory in “Projects” to the workspace.

it’s a little bit of work to setup but it works.

no errors any more