go: go.mod file not found in current directory or any parent directory; see 'go help modules'
I just updated to the new version of Go - Go version 1.16.2 Linux/amd64 and am getting an error when I build a Hello, World! example:
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Even when I follow the fix from that post it isn't working. I set these variables and then build again:
GO111MODULE=on
GOPROXY=https://proxy.golang.org,direct
And the same problem unfortunately.
Change this:
go env -w GO111MODULE=auto
to this:
go env -w GO111MODULE=off
Yes, just follow the tutorial and for me that was doing go mod init test3
to create a module. No one else has been upgrading from the old version or everyone else just understood it properly I guess.
From your project directory, add this line of code in your Dockerfile
file if you are building a Docker image:
RUN go mod init
Or this in your directory:
go mod init
First make sure that your GO111MODULE value is set to "auto". You can check it from:
go env
If it is not set to "auto", then run:
go env -w GO111MODULE=auto
Go to your work directory in terminal and run:
go mod init
go mod tidy
You are good to go!
This worked for me:
FROM golang:alpine
WORKDIR /go/src/app
ADD . .
RUN go mod init
RUN go build -o /helloworld
EXPOSE 6111
CMD ["./helloworld"]