How to create and publish packages in Go 📂

Carlos García Rosales
5 min readFeb 28, 2023

--

¿Hablas español? Ver en español

A necessity in the world of programming has always been to reuse our code and functions, in fact it is one of the golden rules, DRY, in English these acronyms mean do not repeat yourself but many of us have encountered the dilemma to have such a useful class in a project and we thought…

I would like to be able to use this class in this project, ctrl + c and ctrl + v

Then it happens that one of the projects adds an extra function to that class and you realize that it would also be good in the other, you copy the function and test it. This can be an endless cycle, I tell you, you can be copying and pasting functions of the class forever.

The solution is very simple to make that class so useful for you an independent package with its own tests and its own repository that we can use in all the projects that need it.

Let’s prepare the environment

In my case inside my template project that I use for 80% of my projects, I have several sub packages that sometimes I need in other projects, where I decided not to use the template because it was a small project to use that template, so we will take one of them in this case the:

Session Manager: I use it normally to persist the session without depending on the framework I use.

To do this we will start by creating our folder

mkdir session-manager

Then we will create a public repository on GitHub, making it public is very important in the future I will make a new article where we will make a private package, we copy the link of our repository.

And now we are going to initialize our go package inside the folder we created

go mod init github.com/user/repository-name

Followed by those we will initialize the Git repository in the folder we created.

git init

Now we will add two more files inside the folder the README.md and the license in my case I will use MIT.

We upload all our files to the repository and we already have our environment ready to start developing our package.

Now we proceed to move our entity or entities.

Some recommendations that I can leave you is to move only what is strictly necessary for the operation of the package and its tests. Something crucial is to have a package with the greatest amount of Test Coverage.

For my case I will move the interfaces, the structures and the tests. Each of them in different files to be able to find them more easily.

Also something I usually do is move some workflows that I use to check the tests before merging and see the test coverage level, these will go to the .github folder

Our package would be as follows.

I use the file I call main.go to describe the package in a general way.

Add documentation for our package.

In Go we can document our package thanks to godoc.org just by adding formatted comments to our entities.

I’ll give you an example of a documented package using the godoc format:

// Package math is a package with mathematical operation
package math

const (
// PI is the pi number with 12 decimal's
PI float64 = 3.14159265359
)

// Calculator struct who will store like methods all our operations
type Calculator struct {}

// NewCalculator create a new instance of calculator
func NewCalculator *Calculator {
return &Calculator{}
}

// SumInt sum a group of int
func (c *Calculator) SumInt(numbers ...int) int{
total := 0

for _,n := range numbers {
total=total+n
}

return total
}

This format will mean that when we upload our package, godoc.org will format our comments and add them to the documentation section.

Publishing our package.

Now the moment has come, which in my opinion is the most interesting is to see our package already inside godoc.org for them we must prepare our environment once all our progress has been uploaded to the repository we will proceed to create a tag.

git tag v0.1.0

After creating it we will upload this tag to our repository

git push origin v0.1.0

And now we will tell Go that our package is ready to be extracted. We must replace with the link of our package and the version that we are publishing.

GOPROXY=proxy.golang.org go list -m github.com/solrac97gr/session-manager@v0.1.5

Using our package in other projects

Once our package is published we can start using it in other projects for this we will use the go get directive

From another project we will use the following command

go get github.com/solrac97gr/session-manager

Now it will only be enough to import our package and use what we need from it in our other projects.

package main

import github.com/solrac97gr/session-manager

func main() {
user := struct {
Name string
Age int
} {
Name: "Carlos",
Age:26,
}


sm := sessionmanager.NewSessionManager()

s,err := sm.CreateSession()
if err != nil {
panic(err)
}

s.Set("user",user)

sm.SetAsDefaultSession(s.SessionId())

}

Conclusion

Go provides us with a very simple tool to share code between our projects and teams that we must take advantage of. There are many teams that usually solve the need to share code using thousands of microservices. Microservices must be created with great criteria since they generate a spending on infrastructure there are very small things that are not worth making a microservice but by making it a package we solve the need to share those functionalities. I leave you a link to the package that separates from my template that I use this 2023 to program projects in Go so that you can see it, contribute to the project and leave a star

Link to the repository

--

--

Carlos García Rosales
Carlos García Rosales

Written by Carlos García Rosales

I am Software Developer working in @bayonet.io using the amazing programming language Go! I hope help you with my articles. follow me in my social medias!

Responses (1)