Creating a module in Go

Creating a module in Go

In this tutorial, we will discuss understanding the package and also creating a module in go. The below command can be used to create a module.

go mod init {module_import_path}

Let’s see go.mod and go.sum file again that we had discussed earlier tutorial.

Creating a module in Go
go.mod

go.mod is the module dependency file. It will have three things

  • Import path of the module at the top
  • The version of go with which the module is created
  • Direct dependencies of the module.
go.sum

This file lists down the checksum of direct and indirect dependency required along with the version. It is to be mentioned that go.mod file is enough for a successful build. The checksum present in go.sum file is used to validate the checksum of each direct and indirect dependency.

Now the question is, what is import_pathimport_path is the prefix path that any other module will use to import your module. Go to any directory outside $GOPATH/src folder. Let’s say the directory name is waytoeasylearn

mkdir learn
cd learn

Let’s say the module import path is waytoeasylearn.com/learn

go mod init waytoeasylearn.com/learn

This command will create a go.mod file in the same directory. Let’s examine the contents of this file. Do a cat go.mod

module waytoeasylearn.com/learn

go 1.14

When the module is first created using the init command, go.mod file will have two things only

  • Import path of the module at the top
module waytoeasylearn.com/learn

Version of go with which the module was created

go 1.14

Since it is an empty module it doesn’t have any direct dependency specified yet. Let’s create a file named main.go in the same directory with below contents

package main

import (
	"fmt"
	"strings"

	"github.com/pborman/uuid"
)

func main() {
	uuidWithHyphen := uuid.NewRandom()
	uuid := strings.Replace(uuidWithHyphen.String(), "-", "", -1)
	fmt.Println(uuid)
}

Notice the package declaration in above file

package main

This means that the above source file belongs to the main package. Also, Notice that we have imported the dependency in the main.go as well

"github.com/pborman/uuid"

Since main.go file belongs to main package; hence we can create an executable. The executable is always built with the last name in the module’s import path, which contains the main package. 

While running the go install command. In our case, the import path of the module is waytoeasylearn.com/learn, and the last name in the import path is learn. Hence executable will be created with name learn when running go install.

Any of three command can be used to create the executable

  • ‘go install learn’
  • ‘go install’
  • ‘go install .’

The name of the directory would not matter here. The name of the executable will always be the same as the name of the module. 

All the above commands will create an executable name learn in the $GOBIN directory. If the $GOBIN directory is in your path, then you can directory run the executable.

learn

Also, the go install command will download all the dependencies required in your source files and update the go.mod file with that dependency.

After running this command let’s now let’s again examine the contents of go.mod file. Do a cat go.mod

module learn

go 1.14

require github.com/pborman/uuid v1.2.1

It lists direct dependency, which was specified in the main.go file along with an exact version of the dependency as well. Now let’s check the go.sum file as well. Do a cat go.sum

github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=

go.sum file lists down the checksum of direct and indirect dependency required by the module. the github.com/pborman/uuid internally uses github.com/google/uuid. It is an indirect dependency of the module, and hence it is recorded in the go.sum file. 

Both the direct dependency as well as the indirect dependency will be downloaded in the $GOAPTH/pkg/mod/cache folder with versioning.

We can also run the executable directly.

learn

Note

$GOBIN directly needs to be in your path for above command to run.

Output

e594dc4d9a754bcb83b56e89b18b4b46

The above way, we added a dependency in the source file, and when we run go install, it downloaded that dependency and added it in the go.mod file. Also, the executable got created in the $GOBIN directory.

Now couple of points to notice

Name of the directory is learn

Name of the directory which contains the go.mod file doesn’t matter. You can try changing name to anything. It will always create the executable with the same name as last part in module import path.

Import path of the module is waytoeasylearn.com/learn

The executable is always created with the name of the last part in the module import path, which is learn here. How does the import path matter? 

We will learn in the up coming tutorial. For now, understand that the module import path is used to import that module into another module. Also, if the module import path is just one name, then executable will be created with that name only.

For example, it is possible that the module import path would only have been learn. In this case, too executable would have been created with name learn only. So for the below module import paths, the executable name would be learn.

waytoeasylearn.com/manage/learn
waytoeasylearn.com/learn
learn
Name of file is main.go

The name of the file won’t matter here when running ‘go install’. You can try changing the file from main.go to test.go. After doing that run the above commands again. It will create an executable with the module name which is learn and nothing changes.

Name of package in learn module is main

The name of the package does matter. If go install sees a main package in the directory, it will create an executable in the $GOBIN directory. The name of the executable will be the same as the last name in the module import path.

What we learnt so far

  • Only main package is executable.
  • On running ‘go install‘ it creates the binary with the name as the last part of the module import that contains the .go file belonging to the main package.
  • The name of the directory containing the module doesn’t play any role in the name of the executable.
  • The name of the file doesn’t matter when running go install. You can have any name

That’s all about the Creating a module in Go language. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Go language.!!

Creating a module in Go
Scroll to top