is a very new programming language. It was released in September 2009 and it has good support and popularity among the developers. Go has a simple syntax and it has a good feature of concurrency. So, it is suitable for web development. The syntax is quite easy and can be used to write programs.
Go is a compiled programming language. So, we have to use some external libraries to run the programs. But if we want to create our own packages and distribute them, then we will have to compile our package and distribute it. So, the question is how to make a custom go package and distribute it.
![Create, Publish And Distribute Go Packages On Pkg.go.dev 2 Create, Publish and Distribute Go Packages On pkg.go.dev](https://www.codewithc.com/wp-content/uploads/2022/06/Go-Programming-language-1024x578.png)
How To Make Custom Go Packages And Distribute Them?
Are you interested in creating a custom package and publishing it on pkg.go.dev? Here I will explain you how to create, publish and distribute go packages.
Let me start with explaining the basic concepts:
Go Packages
In go, a package is a directory with a name. The file extension is called “go”. The name of the directory can be any name.
The main advantage of go packages is that you don’t need to create a new file for each package that you develop. Instead, you just need to create the package and upload the files.
Creating a Package
When you develop a package, you need to use go tools like go generate, go build and go install.
For example:
$ go generate
$ go build
$ go install
When you run these commands, it will create the package and it will be placed in the current directory.
The go tools will give you a go.tools/… path which is the path of the package that you just created.
So, let us assume that you have a package name as “custom” and you want to create it. So, you can write something like:
$ go generate
$ go build
$ go install.
Now, the output of the command will be like this:
$ go install.
The above command will create the directory custom and it will place the package at the same location.
Now, let us see how to make a simple package.
Adding A New Package
Let us assume that you want to add a new package and you don’t want to create a new directory. So, the first step is to run go tool init.
To run this command, you need to make sure that the directory is empty.
Let us assume that you are in a folder “pkg” and you have a directory “custom”. You can run:
$ go tool init pkg/custom
Now, you will get a window that will ask you to enter the name of the package. You can give any name. You can also add a description. You will also get a list of options. You can choose any of them:
Once you have chosen the option, you will get a window that will ask you to enter a main.go file.
You can also add a readme.txt file if you want to make it more descriptive.
After filling all the details, you will get a window that will show you the options that you have selected.
You can click on the OK button and it will create a directory “pkg” and place the “main.go” and “readme.txt” files inside it.
Now, you will have to run:
$ go install pkg
This will place the new package in the pkg directory.
Distributing Your Package
You can either upload it to github or pkg.go.dev. You can use the following steps:
Create a repo in github
If you want to make your package public, then you can create a repository in github.
Login to github and then create a repository.
Now, you will have to create an account in pkg.go.dev. It is important to mention that if you have an existing account in pkg.go.dev, then you need to log in to the account.
After login, you will get a welcome page. Next, you will get a form that will ask you to enter a title, description, and license. You can also enter a maintainer.
Once you are done with the above, you will get an option to create a new issue. You can also enter a new release in this page. It will ask you to select the tags which you want to include in this release.
Finally, you will get a window that will ask you to upload your package. If you want to upload it to pkg.go.dev, then you can select it from the menu.
Conclusion:
In this post, I have explained to you how to make a custom go package. You can also distribute it on pkg.go.dev. You just need to follow these