Managing Dependencies with Go Modules v2

Yunus Kılıç
3 min readFeb 14, 2020

Inside this post, I will try to explain how to use go modules with a real-world example.

First of all, managing dependencies becomes confusing while using Go, especially for the developers who come from languages such as Java, C#, etc like me. Whole dependencies will be installed to the src directory inside GOPATH. And other kinds of stuff is very unique for Go.

“Go 1.11 and 1.12 include preliminary support for modules, Go’s new dependency management system that makes dependency version information explicit and easier to manage. This blog post is an introduction to the basic operations needed to get started using modules”[1].

In this tutorial, I will refactor one of my previous post repository. And you are going to your life become easier.

https://medium.com/@yunskilic/developing-golang-aws-lambda-functions-with-gorm-amazon-rds-for-postgresql-c5efbcbd0b0d

1-) Clone and build code

$git clone https://github.com/yunuskilicdev/gormAws
$cd gormAws
$rm go.mod go.sum
$git checkout cad5bad924572f23a94dbb38af347becae9bf860
$go build createUserController/main.go

cannot find package “gormAws/db” in any of:

/usr/local/go/src/gormAws/db (from $GOROOT)

/Users/XXX/go/src/gormAws/db (from $GOPATH)

Dependencies were not installed by default. So you need to do lots of manual operations to use the project. But we will use go module. Let’s do it.

2-)Migrating To Go Modules

If you use other dependency management strategies like dep, it will be easier to migrate. But inside that repository, I did not use any of them. But it is going to be all right.

$go mod init

go: cannot determine module path for source directory /Users/XXX/Documents/SourceCode/gormAws (outside GOPATH, module path must be specified)

Example usage:

‘go mod init example.com/m’ to initialize a v0 or v1 module

‘go mod init example.com/m/v2’ to initialize a v2 module

Run ‘go help mod init’ for more information.

It gives an error that states we have to state the module path and version of the go module. The most mature and useful version is v2.

$go mod init github.com/yunuskilicdev/gormAws/v2

go: creating new go.mod: module github.com/yunuskilicdev/gormAws/v2

But build is still unsuccessful because of the project structure.

$go build createUserController/main.go

build command-line-arguments: cannot load gormAws/db: malformed module path “gormAws/db”: missing dot in first path element

Let’s adapt imports with respect to go modules.

Replace import string:

  • gormAws/model

With:

  • github.com/yunuskilicdev/gormAws/v2/model

Replace import string:

  • gormAws/db

With

  • github.com/yunuskilicdev/gormAws/v2/db

Then build again

$go build createUserController/main.go

The build was successful. Build others. All of them fine now.

I am using GoLand as IDE. Builds are fine but IDE still gives errors. You have to enable go module support like the below screenshot.

So our project was converted to go module v2 :)

$ cat go.mo

module github.com/yunuskilicdev/gormAws/v2

go 1.13

require (

github.com/aws/aws-lambda-go v1.14.0 // indirect

github.com/jinzhu/gorm v1.9.12

github.com/joho/godotenv v1.3.0

)

A few commands about go module[2]:

go mod graph

Graph prints the module requirement graph (with replacements applied) in text form

go mod tidy

Tidy makes sure go.mod matches the source code in the module. It adds any missing modules necessary to build the current module’s packages and dependencies, and it removes unused modules that don’t provide any relevant packages. It also adds any missing entries to go.sum and removes any unnecessary ones.

go mod verify

Verify checks that the dependencies of the current module, which are stored in a local downloaded source cache, have not been modified since being downloaded

go mod why

Why shows a shortest path in the import graph from the main module to each of the listed packages.

Thanks for reading :)

Cites:

1-) https://blog.golang.org/using-go-modules

2-)https://golang.org/cmd/go/#hdr-Module_maintenance

--

--

Yunus Kılıç

I have 10 years of experience in high-quality software application development, implementation, and integration.