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

I was working on building a Docker image containing a go application when i ran in to the following error when running go tidy as a build step:
“go: cannot find main module; see ‘go help modules'”

The reason for this was that i had forgotten to specify a workdir in my Dockerimage (Alternatively, specified a go dir). Thus a fail safe error is triggered, as go simply gets confused over finding a go.mod in the gopath root folder, which is supposed to be within your source folder. [source]

The fix? Specify a WORKDIR in your Dockerfile.

Leave a Reply