In this blog, we will learn how to create a module in Golang by following some easy steps. It may seem intricate at first, but it really isn’t. It is not rocket science for which you have to resort to Golang web development. We will start from scratch and cover all the basics.

Let’s build a Module in Golang

Table of Contents

  1. Let’s build a Module in Golang
  2. Prerequisite Knowledge 
  3. Let’s Start to create a module
    1. Create a New Directory 
    2. Start Using VS Code
    3. Creating the go.mod File 
    4. Executing go test 
    5. Adding a Dependency 

Prerequisite Knowledge

  • Package: Just consider the package to be a simple directory with a number of .go files. Now, there may be one or more .go files. This means that each source file (under that directory) will have the same package name Now this package with various source files forms the basic building block of a program in Golang.
  • Module: A module is a collection of packages that are arranged properly in the form of a tree file. And you have the go.mod file at the root. This go.mod file describes the path of the module and tracks the dependencies as well. 

Now that you have a basic understanding of the preliminary concepts, it will be easier for us to go ahead with the tutorial. 

Let’s Start to create a module

Create a New Directory 

Now you may wonder why are we starting with a new directory. Well, we want to build a module that is outside the set GOPATH. Why? This is because we can import the module into any other directory as and when we want to (only after we create the go.mod file). 

  • I typically store all my projects in go-workspace, but we will create a new directory here called ‘comics’. We will save this directory on the desktop. 
  • Now, we open Command Prompt and type the following code:
cd Desktop 
mkdir comics 
cd comics
code .

Now as we type code ., the VS Code opens up. VS Code is the IDE that I will be using in this tutorial. However, if you wish to use any other IDE, that is perfectly fine. 

Start Using VS Code

Now that we have the VS Code open in front of us, we will create two source files inside the directory ‘comics’. These two source files will be named comics.go and comics_test.go. In the comics.go file, we type the following code:

package Comics
func Comics() string {
return “Who is your favorite DC superhero?”
}

The code is simple. And you can clearly see that there is no main function in this code. There is just one function named Comics(). 

In the comics_test.go file, we will write a simple test. For this, you have to type:

package Comics
import “testing”
func TestComics(t *testing.T) {
want := “Who is your favorite DC superhero?”
if got := Comics(); got !=want {
t.Errorf (“Comics ()= %q, want %q”, got, want)
}
}

Just know this, the package testing enables you to automate the testing of packages in Golang. 

  • Now that we have written the test, it is time to run the test. And how do we do so? With the help of the ‘go test’’ command. As we type the command in the Terminal, we get to see a message that goes:

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

(Side Note: Now this is a very important step. Why do you think you were displayed such a message? Well, you do not have the go.mod file. What we essentially have is a Comics package, not a module. Because without the go.mod file at the root, there arises no question of module.)

So, now we have to create a go.mod file that we can import as and when we want into other directories. 

Creating the go.mod File 

In this section, we will learn how to build the go.mod file. Again, we go to Terminal and we type the following command 

go mod init github.com/comics 

As we type this command, we see that a go.mod file has been created. This is confirmed by the picture given below.

When we click on the go.mod file, we get to see the name of the module and the current version of the Go (for me, it is 1.19). You can change the name of the module, i.e, github.com/comics at any point of time. 

Since we have the go.mod file, we can now run the test. 

Meanwhile, if you are working on a Golang project, building REST APIs or web applications, and are finding it difficult, you have a good option at hand. You can hire experts from a Golang web development company to handle all the intricate aspects of the project. Due to their years of experience, the experts will be able to guide you appropriately and deliver the project on time. 

RELATED:

Cool CSS Tricks You Must Know

Executing go test 

In order to type ‘go test’, we have to go to the Terminal again. Once we type go test, we see that the test is successful. Have a look at the picture below.  

Now that we have the module at our disposal, and the test has also been successful, let us try to add a dependency, shall we?

Adding a Dependency 

  • Let us try to add a dependency, gorilla mux router, which is a very powerful HTTP router. For this, we will again go to the Terminal. And we will type the following command 

go get github.com/gorilla/mux 

As we do so, you will get to see another statement in the go.mod file. It states 

require github.com/gorilla/mux/ v1.8.0 // indirect 

So, it downloads the dependency. 

In this way, you can add as many dependencies as you want. 

Were you able to understand the steps you have to follow in order to create a Golang module? Great. In case of any difficulties, all you have to do is revisit the steps and type out the commands on the IDE and Terminal manually. This way you will have a strong grasp of the concept. 

Share.

I am a Full-Stack Web Developer & Security Analyst from Bangladesh. I have built web/online applications on various Open Source Stacks and love information security testing.

Comments are closed.

Exit mobile version