Writing and "publishing" your first package in Go
Lucas Andrade
Posted on January 30, 2024
For a few months now I've been venturing into Go on my company projects. I have much more experience with Javascript/Typescript on the frontend, and on the backend and when writing scripts, I have much more experience with Python. So I'm trying to get used to some of Go's quirks, but there are many reasons why I chose Go as my main personal language. I will leave those reasons for another article.
Oh, I swear, it's not because of the cuteness of the mascot...
Anyway!
Recently I wrote a package that analyzes some data from an url and returns a relation with that data:
{ "LoadTime":"488.837125ms","HTTPRequestsCount":3,"PageSize":53 }
You can check out and use this package here.
In detail, it's not the focus of this article to explain why the package was made, how it was made, how to develop it... My goal is for this reading to end with you understanding how to release a package in Go, so that you can release any package you want.
But without further ado, let's go step by step!
Disclaimer: I'm assuming in this step-by-step guide that you've already created a Go project, whether it's a simple one or a more complex API, so I'll spare you some basic explanations.
Step 1: Defining and creating your package
Let's start with the basics, which is creating the package in Golang. In the example in the article, I'll explain it as if I were creating the package I've already created, url-analyzer itself.
In theory, everything you create in go is a "package". So the first step will be no different from what is already done in any project, which is to start the package in go inside the project folder, either by cloning a repository created on Github, or by creating a folder directly with the project name:
go mod init github.com/your-username/url-analyzer
It will create a go.mod in your package with something like that:
module github.com/your-username/url-analyzer
go 1.21.5
Step 2: Creating your package features
In the root of your project, create a file for the package that contains the functions, classes, or types you want to import externally. It can be called main.go, but it's recommended to create it with the name of your package, since it's not a program to run on its own, so I created a folder called url-analyzer, which contains the only method in my package:
package url_analyzer
import (
"github.com/olucasandrade/url-analyzer/models"
"github.com/olucasandrade/url-analyzer/services"
)
func Analyze(url string) (*models.PerformanceData, error) {
as := &services.AnalyzerService{}
return as.AnalyzeURL(url)
}
In my case, I use other folders for the architecture, such as models, services, and helpers, to facilitate unit testing and organization (you can see more at the repository link), but the same logic applies to any project, even if it's a method that adds two values.
Step 3: Publish your package on Github
With the package itself already created, we just need to follow the steps to host it correctly in your repository. In my case, I used Github.
It's no different than uploading any code:
git add url-analyzer
git commit -m "..."
git push origin main
Unlike Python with Pypi and Node with npm, in Go the download of a package happens directly where it is hosted, making life much easier for those who publish the package.
That's it! Your package is now public and available to everyone.
Importing and Testing Your Package
As mentioned above, in Go, downloading a package happens right where it is hosted, so we can create a project for testing in the same way:
go mod init github.com/your-username/url-analyzer-tests
Then get the package with go get:
go get github.com/olucasandrade/url-analyzer
and in the main.go folder I'll test if my method Analyze
works correctly:
package main
import (
"fmt",
"github.com/olucasandrade/url-analyzer"
)
func main() {
response, err := url_analyzer.Analyze("www.criaway.com")
if (err != nil) {
panic(err)
}
fmt.Println(response.HTTPRequestsCount, "-", response.LoadTime, "-", response.PageSize)
// 3 - 114.285791ms - 53
}
I hope you like it! As mentioned above, I'm also learning Go, so any comments, suggestions or the like are welcome! See you in the next article!
React 5 times to show support 🥰
Posted on January 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.