How to Publish a Golang Package
You might be wondering,
“What happens when I install a package with
go get|install?”
or
“How do I publish a golang package?
Well, let me introduce you.
First Things First
You need golang. You probably have this. If not, install golang.
The package manager is baked into the go cli, via commands go get and go install, etc.
Summary
Most packages are installed via the go packages registry.
Here are the steps to publish one here:
- Create a remote repository
- Create a go package
- Push your package to the repository
- Add your package to the go packages registry
Let’s Go!
1. Create a Repository
First, create a repository in GitHub.
2. Create Your go Package
In your repository, create your main.go entrypoint file:
go mod init github.com/$MY_USER/$MY_REPO
touch main.go
In your main.go file:
package main
import "fmt"
func init() {
fmt.Println("Hello, world")
}
3. Push your Package to the Repository
Add, commit, and push this.
git add .
git commit -m "Init"
git push origin main
git tag v1.0.0
git push origin --tags
4. Add Your Package to the go Packages Registry
And follow the instructions here
