Understanding modules in Golang
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.mod
bit. 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
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 π
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 π.