First, let’s assume we don’t know anything about module. Let’s type
go mod --help .
Oops !!! Our command is not correct :D
go help mod.
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.
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
firstname.lastname@example.org. This indicates that we have successfully downloaded the Godog package. We could start import the package according to code requirements.
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.
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.
Let’s back to Godog.
We can edit the path using
go mod edit.
“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?
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 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?