首页 > 后端开发 > Golang > 正文

How to Publish a Golang Package

Mary-Kate Olsen
发布: 2024-09-19 16:15:09
原创
832 人浏览过

Publishing a package is a good way to share your tool with the world. Someone can import the package and use it in their project, and achieve the functionality you have built. It's quite easy to publish a Golang package compared to other languages. We will do that in this blog. I recently published my GenCLI package you can check it out here.

For demo purposes, I have this CLI project. This AI-powered CLI. It provides you with the answers to your questions via the terminal, built with Cobra and Google Gemini API. Now this project is local, and the only way to run it is to go to the Root of the project and do go run main.go and then use sub-commands. It will work fine, but the issue here is that this is not reliable, every time, we need to go to the project root and run it, ideally, it should run from anywhere on the computer, just like other CLI tools. Plus, not everyone will do this much hassle to use it. So, this is why it's necessary, as well as important to publish the tools.

There can be multiple ways to name your package, but as we will host it on GitHub we will use the GitHub way. Make sure in your go.mod file your module name is the following convention - github.com//, just like below.

One thing to note is that the module is a collection of Packages. We generally say Publishing a Module Not Packages, but to keep it simple we kept it that way.

module github.com/Pradumnasaraf/go-ai

go 1.22

require (
....
....
....

)
登录后复制

Now once everything is set push your code to GitHub. Make sure, you push the code to the same GitHub username and repo name you mentioned in the module.

Once you push the code to GitHub now it's time to publish your package, but before you do, here are a couple of best practices you should follow

  • License: Have a license and try to place a minimal restriction on it so that it can easily be used, modified, and redistributed.

  • Documentation: We can comment on the top of the package file to explain the functionality and golang takes this as general package documentation and shows it under the Package documentation section.

  • Tags: Tagging is good when a person has a particular version of the package also tagged versions give predictable outcomes during builds. Tags should follow Semver. Also, try to release stable versions with 1.0.0 and above, this gives developers confidence.

Now to publish the package head over to the URL https://pkg.go.dev/github.com/. In my case, it would be https://pkg.go.dev/github.com/Pradumnasaraf/go-ai. When you visit, you will see a request button; click on that to request adding the package to pkg.go.dev. I will not because I don't want to publish this tool as it was just for a demo.

How to Publish a Golang Package

Once you’re done, after a few hours, it will be on the website. Once it’s live, you can download the CLI by using the go install command:

go install <repo-url>
go install github.com/Pradumnasaraf/go-ai@latest
登录后复制

That's it for this blog. I'm glad you're still reading and made it too. Thank you! I sometimes share tips on Golang on Twitter. You can connect with me there.

以上是How to Publish a Golang Package的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!