How to Create a Module in Golang?

how-to-create-a-module-in-golang

In this post, we will discuss how you can create a module in Golang. It will be a significant step in your journey to master Golang web development.

Here, it is expected that you are aware of the concepts of modules, packages, and go.mod files. Well, if you aren’t, then it is wise to develop a clear understanding of these topics first. However, to make it easy for you to follow this quick guide, we will discuss how to create a module in Golang from scratch.

So without further ado, let us begin.

A Quick Introduction to Modules, Packages, and go.mod File

(Intermediate learners can skip this section and move on to the next section to learn about module creation)

  • First, let us understand what a package is. To put it in simple terms, it refers to a directory inside the Go workspace that consists of more than one source file. Each of these source files will belong to the same package.
  • The packages go on to form the primitive building blocks of the Golang program. With the help of these packages, you can reuse the code, which is a prominent feature in Golang.
  • A module represents Golang package collection in a file tree, with versioning built-in and dependencies. And you have the go.mod file at the root of the tree. The go.mod file is crucial as it specifies the path of the module and the dependency requirements as well.

How to Create a Module in Golang?

  • Usually, we store all the projects and Golang files inside the directory go-workspace, but to help you understand better, let us create a new directory other than the original. We are doing so because it will allow us to define a go.mod file to create a module and import the same to any other directory.
  • Let us name this directory ‘tutorial’ and save it on the desktop itself. You can run the following code in the Command Prompt to do the same:
cd Desktop 

mkdir tutorial

cd tutorial 

code .

As we run the code . command, it will open the VS Code. (Make sure you have already installed VS Code on your system).

  • We are using the VS Code here. However, you can choose any other IDE like GoLand, LiteIDE, JetBrains Fleet, etc.
  • Now, create a source file and name it greetings.go inside the directory ‘tutorial.
  • We need to add the following code in the greetings.go file:
package Tutorial 

func Greetings() string {

return “Greetings, everyone!”

}

As you can see, this is a very basic code and we have not included the main function inside this code. We have only created a function called Greetings(). 

  • Now, we need to create another source file in the directory tutorial. Let’s name this source file greetings_test.go. In the greetings_test.go file, we concoct a test by adding the following code:
package Tutorial 

import “testing”

func TestGreetings(t *testing.T) {

want := “Greetings, everyone!”

if got := Greetings(); got !=want {

t.Errorf (“Greetings ()= %q, want %q”, got, want)

}

}

As you can visualize, we have imported a package ‘testing’ which is one of the most important packages in Golang. It helps to automate tests of the Golang packages and is generally used with the ‘go test’ command (which we will be using ahead).

  • If we run the go test now in the Terminal, we will get the following message:

go: go.mod file not found in the current directory or any parent directory; see ‘go help modules’

(Note: the ‘go test’ helps in the automatic execution of a function of any form

Now you may be wondering why we are getting this message. Well, this is because right now, we have a package tutorial, and it is not a module. Since we do not have a go.mod file at the root of the file tree, we cannot call it a module, and thus we cannot import it into another directory. For this, it is necessary that we create a go.mod file. 

Creating the go.mod File

  • In order to make the current directory the root of the module, we have to use go mod init command.

(Note: the go mod init is a command that helps in the creation of the go.mod file, which helps in tracking the dependencies of the code)

We need to run the following command in the Terminal:

go mod init instances.com/greetings

After running the above command, we can see that a go.mod file has been created.

As you can see below, you will find the go.mod file at the root, along with two source files that we had created earlier, i.e, greetings_test.go and greetings.go.

  • Now, we need to run the test again by typing go test in the Terminal.

We can see that the code has passed the criteria. 

With this, you have been successful in creating a Go module and running a test on it. If you click on the go.mod file, you will see the name of the module and the version of Go that you are currently using, as shown in the following screenshot:

Now, if you wish to use the module in any other directory, you have to import it. The main purpose of the package is to facilitate the reusability of a code.

The reuse of the code block saves a lot of testing effort while developing applications. This is because it has already been tested earlier. You do not need to write and run any additional tests, each time you run it, the full test suite will run automatically.

To Conclude it All

As you can see, it is quite easy to create a module in GoLand. If you follow the steps mentioned above properly, you will create a Go module without any issues.

Pro Tip: It is better to type the code manually and see for yourself how the module is created. In this way, you will have a better understanding.

Good luck!

Share Your Thoughts, Queries and Suggestions!