How to Publish Your Go Library on Pkg Go

Continue my way to learn Go in a deep way, today I’ll show to you how to publish you library in Pkg.Go

First of all, you need to have a Go library in you Github account or any other Git repository. In this example, I’ll use a simple library that I created to show how to publish it.

The library is valid-brazilian-cnpj. In Brazil we have a document called CNPJ, the next year the rules to validate it will change, so I created this library to help me to validate CNPJ.

How I create this library is not the focus of this post, but if you want to see the code, you can access the repository.

To publish your library in Pkg.Go, you need to create a release in your repository.

First Step - run go mod tidy

The first step is to run go mod tidy to make sure that all dependencies are in the go.mod file.

go mod tidy

Second Step - Run tests

The second step is to run the tests to make sure that everything is working as expected.

go test -v

This command will run all tests in the library and show the results.

Third Step - Create a tag

The third step is to create a tag in your repository.

git tag v0.1.0
git push origin v0.1.0

This command will create a tag called v0.1.0 and push it to the repository.

Fourth Step - Push this to Pkg.Go

GOPROXY=proxy.golang.org go list -m github.com/linuxsoares/valid-brazilian-cnpj@v0.1.1

Explaining the command:

  • GOPROXY=proxy.golang.org: This is the URL of the Pkg.Go
  • go list -m github.com/linuxsoares/valid-brazilian-cnpj@v0.1.1: This command will push the library to Pkg.Go

After running this command, you can access the library in Pkg.Go.

Conclusion

In this post, I showed you how to publish your library in Pkg.Go. This is a simple process, but it’s important to follow the steps to make sure that everything is working as expected.

I hope you enjoyed this post and see you in the next one.

Happy coding!