Understanding modules in Golang

Nikasulo
Geek Culture
Published in
6 min readAug 1, 2021

--

Hola amigos! πŸ‘‹ In my previous article, Understanding Packages, we talked about what a package is, the potential structure of packages, splitting packages into multiple files, exporting methods and functions, etc.

Today, we are going to take it a little further and discuss modules and how they tie packages together.

We can think about modules as collections of packages, this means that a module is essentially just a folder in which we have one or more packages. A module can be made up of a single package, in this case, the module and the package name will be the same. Enough talk πŸ˜‚ let’s see some examples πŸ‘‡πŸ½

Here we have a calculator module that contains a few different packages to handle some specific operations and display the results of those operations. We also have a strange file that reads go.mod, if you come from a javascript background, you can liken this to the package.json file in your projects or the Gemfile in your ruby projects, it essentially holds content that describes the module β€” in this case, the calculator module and lists the dependencies of all the packages in that module, it may look something like πŸ‘‡πŸ½

How did we get here?

Let’s write a small module, we’ll call it calculator, and we will use it in another Go program. If you don’t already have Go installed on your machine, go here and follow those nicely written instructions πŸ˜‰

Next, create a new directory where you want to store your code, mine will be Desktop/Playground/go

You’re probably wondering about the go.modbit. If you work in javascript, generating a go.mod file is similar to running yarn init or npm init , you simply do πŸ‘‡πŸ½ in your terminal

The module name should ideally be a place on the web, where your source code is going to be stored, the reason is for your package to be reusable by other developers, you need to tell the Go tools where to find and download it(we will see more on this in a second πŸ‘Œ ).

Next, create some packages in your calculator module πŸ‘‡πŸ½

Let's add some operations, we’ll create simple.go in the operations directory πŸ‘‡πŸ½

Inside of simple.go put the following code πŸ‘‡πŸ½

Notice that our package namespace corresponds to the directory where this file lives and that the functions all start with capital letters, if you’re not sure why, take a look at this article.

Let’s write some code to display the results, from your calculator directory, run πŸ‘‡πŸ½

Inside of format.go let’s add the following code

We will need to make some modifications to simple.go , let's import our format package and use it to add 2 new functions to simple.go πŸ‘‡πŸ½

Here we have added 2 new functions that print out the steps used in the operation– AdditionWithSteps and MultiplicationWithSteps , we have also refactored a little.

Now let’s make this package available for use in other Go programs. Push it to the repository you’ve created for your project. I am using GitHub to store my project and my repository name is calculator

My repository for this project

Now I will commit and push my code to Github πŸ‘‡πŸ½

If all goes well, when you refresh your browser, your repository page should look like πŸ‘‡πŸ½

Now your package is available to whoever wants to use it! Few things to not here, make sure your repository is not private if you intend for people to use your package otherwise, importing it might fail. Try to avoid hyphens and other special characters when naming your module and repository. Each repository should only contain one module otherwise you will run into issues when you start importing your module for use.

Let’s write some go code to use our module and packages! πŸš€ In the root of your projects directory, make a new module call it whatever you want, I’ll call mine simplego πŸ‘‡πŸ½ and inside of that main.go for our code

Inside of main.go, we can drop in the following code πŸ‘‡πŸ½ we are importing the operations package that we created earlier in our calculator module.

If all goes well you should be seeing some errors, similar to the one below. This is because we technically do not have this package installed locally for use. We need to tell go to get it, I mean, literally tell go to get it πŸ˜‰

Error at the bottom of the screen showing that the package is not available to us locally

In your terminal, initialize a new go module file, and tell go to tidy your dependencies πŸ‘‡πŸ½ run this in the root of your simplego module, you should get the output in the comments.

What has just happened? 😱 Well, some beautiful go magic, whenever we run go mod tidy , go looks through all our import statements and downloads all missing packages/dependencies. Our go.mod file should now look like this πŸ‘‡πŸ½

Now we can use operations it in our program, let’s add some code to main.go πŸ‘‡πŸ½

Let’s run our program, in your terminal run the following command and you should see the steps of the operation 1+2+3+4+5 printed out in the terminal window ✌️

It lives! πŸ‘»

Summary

So far, we have:

  • Written a small Go module that can perform some arithmetic and print out the results
  • We published a package
  • Imported our published package into another simple program and used it

In closing, there are a few things I didn’t go over in this article, for instance, the recommended versioning and publishing workflows for Go modules and packages, however, there are pretty great resources, just for that, here and here.

Here, you will find the code for simple.go , and the code for our calculator module can be found here.

Thanks for following along and let me know if you have questions or some feedback πŸ˜‰.

--

--

Nikasulo
Geek Culture

I build for the web. I teach people to build for the web. You can follow me closely here https://twitter.com/nervousrubyist, ask me questions about code πŸ˜‰