How to Fix go: go.mod file not found in current directory or any parent directory

To fix the go: go.mod file not found in current directory or any parent directory error in Golang, you can create a go.mod file using the go mod init <module_name> command or use this command: go env -w GO111MODULE=off.

The “go: go.mod file not found in current directory or any parent directory” error occurs when you try to use Go modules, but there isn’t a go.mod file in the current directory or any parent directory.

Let’s initialize a go module using the below command in your project’s root directory:

go mod init <module_name>

Replace <module_name> with a name for your module. Typically, the module name is the repository URL where your code will be hosted. For example:

go mod init github.com/yourusername/yourproject

This command will create a go.mod file in the current directory with the provided module name. The go.mod file manages your project’s dependencies and other module-related settings.

After initializing your project as a Go module, you can use Go commands like go build, go test, and go run without encountering the go.mod file not found error.

That’s it.

Leave a Comment