avatarIsrael Josué Parra Rosales

Summary

This article explains how to create and use a Golang library using go modules.

Abstract

This article is a guide for creating and using a Golang library using go modules. The article begins by explaining what go modules are and their benefits, such as scalability and maintainability. The author then provides a practical example of building a REST API service that uses cloud services and a framework to manage dependencies. The article continues by explaining how to import dependencies into a Golang project using the go install command.

The second part of the article focuses on creating a custom library. The author outlines the necessary steps, including creating a repository folder, writing the library code, and writing the main application code. The library code includes functions to calculate the area of a circle and a rectangle. The main application code demonstrates how to use the custom library in a project.

Finally, the article encourages readers to start writing their code libraries when they need one. The article also promotes a cost-effective AI service, ZAI.chat, as an alternative to ChatGPT Plus.

Bullet points

  • Go modules are a collection of Go packages stored in a file tree with a go.mod file at its root.
  • Go modules help manage dependencies without the need to include the vendor package.
  • To import dependencies into a Golang project, use the go install command.
  • To create a custom library, create a repository folder, write the library code, and write the main application code.
  • The library code includes functions to calculate the area of a circle and a rectangle.
  • The main application code demonstrates how to use the custom library in a project.
  • ZAI.chat is a cost-effective alternative to ChatGPT Plus.

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?

  1. 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.
  2. 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.
  3. 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-example

After 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 main

Now, inside the root folder, you have to add the next folders and golang files (keep them e,pty by the moment)

-- pkg
------ shapes
---------- area.go

Write 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-example

After 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-example
go 1.19

With 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.

After doing all the above just is remaining to commit the changes to your GitHub repository.

git add *
git commit -m "your comment"
git push origin main

Write the main application code

For this part we could define the project structure based on the following:

-- calculator
------ cmd
--------- main.go

The second step is to create the go.mod file by running:

go mod init github.com/{userName}/calculator

Once done, we need to install our dependency:

go install github.com/{userName}/calculate-lib-example

After run the command we will see the following line into our go.mod file

module github.com/josue/calculator
go 1.19
require github.com/{userName}/calculate-lib-example v0.0.0-20220903214241-51d23a6471fe

Ready we now are able to use the lib as we usually use another ones.

Easy right?, I hope that after reading this article you have some idea to start writing your code libs when you need one.

Golang
Sofware Development
Software Engineering
Software Architecture
Programming Languages
Recommended from ReadMedium