GO-vatar series: Introduction to the Go Module

Ferawati Hartanti Pratiwi
5 min readFeb 14, 2021
https://miro.medium.com/max/1200/1*LutJiAc3Wdg-5GtOpP9YXw.png

“A module is a collection of Go packages stored in a file tree with a go.mod file at its root” — https://blog.golang.org/using-go-modules

If you’ve seen my golang-automation repository, there is a file named go.mod.

What is Go module? Let’s learn more about module in Go !

Help

First, let’s assume we don’t know anything about module. Let’s type go mod --help .

Oops !!! Our command is not correct :D

Let’s try go help mod.

Init

I created new project outside $GOPATH. When I try to generate new module, I’ve got this.

We have to add some path where our project located.

Then we can see that go.mod file has created successfully.

Download

Some of our code might need another package outside internal package from Golang. In this way, we can provide dependency on the packages we need.

For an example, I will demonstrate how we download Godog for our example module. See picture above, there are 3 folders for Gherkin.

After we type go mod download then back to folder where Gherkin folder located. There is folder godog@v0.11.0. This indicates that we have successfully downloaded the Godog package. We could start import the package according to code requirements.

Tidy

We’ve download too many packages until we’re not realize that actually we don’t need some of the packages. Be calm. We only need to run go mod tidy. This will keep the module up to date with the source code needed. We just downloaded the Godog package, but we’re not called it yet. Do we need that package? I think no. Let’s tidy it.

Tidy not only remove unused package but it also add the package that actually we need.

Vendor

So we removed the Godog package. Let’s change the code.

We need to load the .env file using Godotenv package. After we run go mod tidy once again, the go.mod looks like this.

Pay attention to Create vendor directory. If we’re doing go get or go mod download, it will copy the package outside our workspace folder. But if we want to collect the package to the workspace folder, we can do it by go mod vendor.

go mod vendor result. The pckage is copied to workspace folder

Edit

https://golang.org/ref/mod#go-mod-edit

Let’s back to Godog.

Previous version.

github.com/DATA-DOG/godog

Current version.

github.com/cucumber/godog/cmd/godog

We can edit the path using go mod edit.

Graph

“print module requirement graph”. For example, we require 1 or 2 package in go.mod file.

This will produce graph like this.

If we have another package. Let’s write Godog package.

Then the the result will be like this.

Different, isn’t it?

Verify

We could do this after go mod download. This to make sure go.sum file has matches with module we’ve downloaded. What is go.sum file?

go.sum is a file lists down the checksum of direct and indirect dependency required along with the version. We could use go.sum to validate the checksum of each of direct and indirect dependency to confirm that none of them has been modified. — https://golangbyexample.com/go-mod-sum-module/.

Above picture means that my go.sum is in accordance with the package that I downloaded earlier.

Why

Why we need the packages or the modules? We can sneak a peek the info with this command.

We don’t actually need the package Godog. So better remove it, right?

https://i.ytimg.com/vi/rBZOOtEsbxg/maxresdefault.jpg

Having fun? There a lot of things I don’t know about Golang. Please share with me. :) — MperMperPisang

References :

--

--

Ferawati Hartanti Pratiwi

QA (Quality Ambassador) | mpermperpisang official Medium account