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.
Table of Contents
ToggleAMP(Intermediate learners can skip this section and move on to the next section to learn about module creation)
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).
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().
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).
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.
(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/greetingsAfter 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.
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.
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!
In today’s competitive landscape, around 4.4 million blogs are published every day and what sets us apart is that the Golang company loves sharing our thoughts around Go that is useful for both developers and business owners interested to know more about Golang.