Chapter-2-Fix the error (go.mod file not found)

Hexnos
4 min readJul 21, 2023

--

In our previous chapter, we learned to code our first hello world program. But we encountered an error “ go.mod file not found ”.

So in above image, you can see, compiler throws an error go: go.mod file not found in current directory or any parent directory.
Actually compiler is looking for the “ go.mod ” file in current directory, in our case we don’t have. In order to fix it we need to create this mod file.

So here question is what is that mod file ?

I’ll try to answer this question in simple form.
Suppose we are building an app, and started writing our code. Now our program needs the third party functionalities or any features then we use external libraries/dependencies.
For Example : We need a facebook login functionality in our app so in this case we’ll import facebook library.

import (
"golang.org/x/oauth2"
facebookOAuth "golang.org/x/oauth2/facebook"
)

Above import code is just for demonstration of how we include third party libraries. After including this library, we need to download it by using “ go get command” in terminal :
go get golang.org/x/oauth2/facebook
then your facebook library will be imported in your project.

But the problem is, when you have multiple third party libraries, assuming 10 library in a project, So manually you’ll have to give command “go get [library]” for each library and you’ll end up with a long break.

So in order to save time and effort to import libraries and files, we use “ go.mod ” file. Once you created the mod file then compiler will fetch all those required libraries for you, then you don’t have to manually fetch the libraries through terminal.

go mod init project_name initializing the go.mod file that describes the module. At beginning, it will only add the path of the module and go version in go.mod file.
This also creates a go.sum file which maintains the checksum so whenever you run the project multiple times, it will not install all packages again and again. It uses the cache which is stored inside $GOPATH/pkg/mod directory (module cache directory).

So I guess, now you know why this error occured and why we need go.mod file in our project.

so lets create this go.mod file and we’ll use our previous hello world program.

package main

import (
"fmt"
)

func main() {
fmt.Println("Hello World!")
}

We will continue from here (Hello World), and will create the go.mod file.

Open your terminal (Ctrl + Shift + `) or Terminal → New Terminal. Skip if the terminal is already opened your VS Code Editor.

Now type this command and hit enter :

go mod init notesapp

Above command Explained :
This “ go mod init ” initialize the go module and “ notesapp ” is your current project directory name.

This is an important step else you’ll end up with an error while creating go.mod file.

Suppose you’ve created a folder “notesapp” in your machine and main.go file is there in this folder, the “ go mod init notesapp ” should be the command.

Hit enter and the output should be →

Here in above image you can see mod file is created. But there is one thing you’ll notice “go mod tidy ”, so what is that “ go mod tidy ” ?

go mod tidy is a command that synchronizes the go. mod file with the actual dependencies used in the codebase. This command ensures that the go. mod file contains the correct dependencies and versions used in your codebase.

So after generating the mod file just enter this command go mod tidy and all the dependencies will be synchronized.

Now the go.mod not found error will be fixed after creating this file. and lets run our program again. Run -> Start Debugging (Func + F5 or just F5).

So in debug console we got the result.

Next > Chapter — 3 (Coming Soon)

Chapters →

Go Intro & Installation
Chapter — 1
Chapter — 2

--

--