go.mod file not found [SOLVED]

In this blog, I will try to cover the fundamentals of the go module, go.mod file, and how to solve go.mod file not found.


Overview on go.mod file in golang

A module is a collection of one or more related packages that provide a discrete and useful set of functions. For example, you could create a module with packages that contain functions for performing financial analysis so that others who are developing financial applications can use your work. See the Developing and publishing modules for more information.


Go code is divided into packages, and packages are further divided into modules. Your module specifies the dependencies required to run your code, such as the Go version and a set of other modules.

To start your module, use the go mod init command:

go mod init [module-path]: The go mod init command initializes and writes a new go.mod file in the current directory, in effect creating a new module rooted at the current directory. The go.mod file must not already exist. init accepts one optional argument, the module path for the new module. See Module paths for instructions on choosing a module path. If the module path argument is omitted, init will attempt to infer the module path using import comments in .go files, vendoring tool configuration files, and the current directory (if in GOPATH).

$ go mod init example.com/golinuxcloud
go: creating new go.mod: module example.com/golinuxcloud

If you are not in GOPATH directory, make sure to enter the path or this error will appear:

go: cannot determine module path for source directory C:\Users\admin\Desktop\golang\golang3\greetings (outside GOPATH, module path must be specified)

The go mod init command creates a go.mod file to track your code's dependencies. At first, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go.mod file will list the versions your code depends on. This keeps builds reproducible and gives you direct control over which module versions to use.

And now you can install any libraries or packages using go get command:

PS C:\Users\admin\Desktop\golang\golang3\greetings> go get github.com/spf13/viper
go: added github.com/fsnotify/fsnotify v1.5.4
go: added github.com/hashicorp/hcl v1.0.0
go: added github.com/magiconair/properties v1.8.6
go: added github.com/mitchellh/mapstructure v1.5.0
go: added github.com/pelletier/go-toml v1.9.5
go: added github.com/pelletier/go-toml/v2 v2.0.5
go: added github.com/spf13/afero v1.8.2
go: added github.com/spf13/cast v1.5.0
go: added github.com/spf13/jwalterweatherman v1.1.0
go: added github.com/spf13/pflag v1.0.5
go: added github.com/spf13/viper v1.13.0
go: added github.com/subosito/gotenv v1.4.1
go: added golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
go: added golang.org/x/text v0.3.7
go: added gopkg.in/ini.v1 v1.67.0
go: added gopkg.in/yaml.v2 v2.4.0
go: added gopkg.in/yaml.v3 v3.0.1

And the go.mod file:

module greeting

go 1.19

require (
	github.com/fsnotify/fsnotify v1.5.4 // indirect
	github.com/hashicorp/hcl v1.0.0 // indirect
	github.com/magiconair/properties v1.8.6 // indirect
	github.com/mitchellh/mapstructure v1.5.0 // indirect
	github.com/pelletier/go-toml v1.9.5 // indirect
	github.com/pelletier/go-toml/v2 v2.0.5 // indirect
	github.com/spf13/afero v1.8.2 // indirect
	github.com/spf13/cast v1.5.0 // indirect
	github.com/spf13/jwalterweatherman v1.1.0 // indirect
	github.com/spf13/pflag v1.0.5 // indirect
	github.com/spf13/viper v1.13.0 // indirect
	github.com/subosito/gotenv v1.4.1 // indirect
	golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
	golang.org/x/text v0.3.7 // indirect
	gopkg.in/ini.v1 v1.67.0 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect


How to solve “go.mod file not found” error?

We have to work with GO111MODULE environment. The go command now builds packages in module-aware mode by default, even when no go.mod is present. This is a big step toward using modules in all projects. It’s still possible to build packages in GOPATH mode by setting the GO111MODULE environment variable to off. You can also set GO111MODULE to auto to enable module-aware mode only when a go.mod file is present in the current directory or any parent directory. This was previously the default.


Solution-1: Create go.mod file using “go mod init”

Make sure that your GO111MODULE value is set to "auto". You can check it by the command:

$ go env GO111MODULE

If it is not set to "auto", then run:

$ go env -w GO111MODULE=auto

Go to your working directory then then try to create go.mod file:

$ go mod init [your-working-folder]

For example if your working directory is /opt/goexamples/helloworld then

$ cd /opt/goexamples/helloworld
$ go mod init helloworld

This will create a go.mod file for helloworld package.


Solution-2: Update the GO111MODULE environment

This was previously the default. Now you have to set GO111MODULE permanently with go env -w:

$ go env -w GO111MODULE=off

GO111MODULE=off forces Go to behave the GOPATH way, even outside of GOPATH



In this article, I showed you how to use go.mod file as well as how to fix go.mod file not found error. Hopefully through the article, you have a better understanding of how to organize code and modules in golang.





Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment