Create a Golang Lib
Working with go modules

In this article, you going to learn how to create your own libs using go mods and import it to the needed projects.
Firts let me give you a little introduction about what the gomodules are.
“A module is a collection of Go packages stored in a file tree with a go.mod file at its root. The go.mod file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build.”
In other words, a go module will help us to get dependencies to your project without the need to include the vendor package. By using golang modules our project is more scalable and maintainable because if a new version of some of our libraries is published we can update easily keeping the new version in our implementations.
Now think of one practical example, we are building a REST API service that uses some cloud services, then we opted to use a framework to make easy the API process and we opted to use an external SDK to connect with cloud services, that will help us to don’t create all the code from scratch, because we going to use the could provider Golang libs, and a very good Rest API framework. To manage that, we going to use go mod to handle the dependencies.
Then, how we can import dependencies to the project? The way to import dependencies into your golang project is by executing the following command `go install the provider.com/repo/package`, that going to get the library and will add a line related to the library and the used version to our go.mod file, and we going to be able to use it in our project.
Creating our own lib
Cool!, at this point you know about the go mod, but now we going to talk about how we can create our own libs, and how we could use them on different projects.
Sometimes we create software solutions that could help us to handle common behaviors in different services then we could move that codes to lib and use them in the services that we want. But how do we do that? ready let’s start.
What are we going to need?
- Create the lib repository. - For this point, we going to be using Github to store our base code, which will help us to manage the lib versioning.
- Write the lib code. - For this tutorial we just going to define two functions to calculate the shape’s area, in this case, a circle and a rectangle.
- Write the main application code. - Here we going to be using the lib code.
Create the lib repository
create a repository folder:
mkdir calculate-lib-exampleAfter that, we need to push the code to our github account to create the repository.
echo “# calculate-lib-example” >> README.md
git init
git add README.md
git commit -m “first commit”
git branch -M main
git remote add origin [email protected]:{accountName}/calculate-lib-example.git
git push -u origin mainNow, inside the root folder, you have to add the next folders and golang files (keep them e,pty by the moment)
-- pkg
------ shapes
---------- area.goWrite the lib code
The first thing that we need is to add the go.mod file, to do that we need to run the following:
go mod init github.com/{userName}/calculate-lib-exampleAfter running we going to see a new file go.mod which is autogenerated in the root folder with the following content:
module github.com/{userName}/calculate-lib-examplego 1.19With that file, we are indicating that we are using go modules in our project.
Now in the area.go file we just going to define two functions to calculate areas, one for rectangles and another one for circles.






