Golang: Understanding Modules

Adrian Silva
3 min readMay 29, 2019

Golang is one of the most approachable, easy to learn, and popular programming languages out there. It is like a combination of Python and C++. However, what these two languages lack is a dependency management system. Surely, you must have heard about Npm which solves the problem of dependencies but it only works with Node.js. With the introduction to modules in the last release, go has become an indispensable tool for all coders who are looking for a language that has it all.

Golang works around with packages which encapsulate all your go files and directories into one single container. Packages can be referenced within your application giving you a whole lot of advantage in code manipulation and reusability.

However, what happens when we want to use a package that is in a remote repository like Github and it has more than one version? What if we want to use a specific version of this repository because it’s compatible with our application? This type of questions led to some developers work around this issue and they came up with modules for Golang.

Think about modules as containers for your packages. When you work with modules, go will automatically create a go.mod file. This file’s purpose is to store all of the packages' import path, including internal and external paths. However, we cannot work with modules inside the $GOPATH/src. So our solution is to create a directory somewhere else. For this example, I will create it in my Home directory.

Now we need to have a go file inside of a subdirectory. I will call this go file rect.go and our subdirectory rectangle. In our file, we can just have a simple structure with a few methods. This package will make use of the fmt package.

package rectangletype Rectangle struct {    Height, Width int}//CreateRectangle creates a new instance of the rectangle typefunc CreateRectangle() Rectangle {    return Rectangle{        Height: 0,        Width:  0,    }}func (rect Rectangle) FindArea() int {    return rect.Height * rect.Width}func (rect Rectangle) Resize(width, height int) Rectangle {    return Rectangle{        Width:  width,        Height: height,    }}

At this point, we can transform our directory into a go module by simply running one command.

The console will have an output that will say “creating new go.mod”. When you see this it means that you have successfully created your first go module. Congratulations!

Now if we take a look at our go.mod file we will see two things: Our current path to our working directory, and our go version

module github.com/xdragon1015/modulesgo 1.12

Now it’s time to test our rectangle package.

package mainimport (    "fmt"    "github.com/xdragon1015/modules/rectangle")func main() {    rect := rectangle.CreateRectangle(10, 12)    fmt.Println("Area of rectangle is:", rect.FindArea())}

Notice that we are running our main file outside of our $GOPATH. This is one of the abilities that modules give us.

--

--