"How to Create and Publish Your Own Go Package"

Sachin BorseSachin Borse
4 min read

Have you ever wanted to create and publish your own Go package? Whether it’s to share your code with others or to reuse it across projects, publishing a package in Go is simple and can be a great learning experience. In this article, I'll walk you through creating a basic package in Go, publishing it on GitHub, and using it in another project.

We’ll create a basic math package with simple addition and subtraction functions.

Step 1: Create the Package

Let's start by creating our package. We'll call it mathops.

1.1 Set Up Your Directory

First, create a directory for your package:

mkdir mathops
cd mathops

This directory will hold all the code for your Go package.

1.2 Initialize the Go Module

Next, initialize your Go module. This will allow us to manage dependencies and versioning easily.

go mod init github.com/yourusername/mathops

Make sure to replace yourusername with your actual GitHub username. This step sets up a go.mod file, which tells Go where to find your package when it’s used by others.

1.3 Write the Package Code

Now, let's write some simple code. We’ll create a file called mathops.go:

package mathops

// Add adds two integers and returns the result.
func Add(a, b int) int {
    return a + b
}

// Subtract subtracts the second integer from the first and returns the result.
func Subtract(a, b int) int {
    return a - b
}

Here, we’ve created a small package that provides two functions: Add and Subtract. This is enough to demonstrate how to create and use a Go package.

1.4 Write Tests

Testing is a good habit to get into. Let's write a couple of simple tests for our functions. Create a new file called mathops_test.go:

package mathops

import "testing"

func TestAdd(t *testing.T) {
    result := Add(3, 4)
    expected := 7
    if result != expected {
        t.Errorf("Add(3, 4) = %d; want %d", result, expected)
    }
}

func TestSubtract(t *testing.T) {
    result := Subtract(10, 4)
    expected := 6
    if result != expected {
        t.Errorf("Subtract(10, 4) = %d; want %d", result, expected)
    }
}

This test file checks that our Add and Subtract functions work as expected.

1.5 Run Your Tests

You can run your tests by executing the following command:

go test

If everything is working correctly, you should see output that looks like this:

ok     github.com/yourusername/mathops    0.001s

Step 2: Publish the Package

Now that we’ve written and tested our package, it’s time to publish it so others can use it.

2.1 Create a GitHub Repository

Go to GitHub and create a new repository called mathops. Don’t initialize the repository with a README, .gitignore, or any other files, as we’ll push our local code to GitHub.

2.2 Push Your Code to GitHub

Back in your terminal, run the following commands to push your code to GitHub:

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourusername/mathops.git
git push -u origin main

Again, replace yourusername with your actual GitHub username. This will upload your code to GitHub, making it publicly available for others to use.

Step 3: Use the Package

Now that the package is published, you can use it in other Go projects.

3.1 Create a New Project

Start by creating a new directory for your project:

mkdir myproject
cd myproject

3.2 Initialize the Go Module

Initialize a Go module for your new project:

go mod init myproject

3.3 Import the Published Package

In your project, create a main.go file and import your mathops package:

package main

import (
    "fmt"
    "github.com/yourusername/mathops"
)

func main() {
    // Use the Add function from the mathops package
    sum := mathops.Add(10, 5)
    fmt.Println("10 + 5 =", sum)

    // Use the Subtract function from the mathops package
    difference := mathops.Subtract(10, 5)
    fmt.Println("10 - 5 =", difference)
}

This code imports your mathops package and uses its Add and Subtract functions.

3.4 Download the Dependency

Next, tell Go to fetch your package by running:

go get github.com/yourusername/mathops

This command downloads your package and adds it to your go.mod file.

3.5 Run Your Program

Finally, run your program:

go run main.go

You should see output like this:

10 + 5 = 15
10 - 5 = 5

And that’s it! You’ve successfully created, published, and used your own Go package.

Conclusion

Creating and publishing a Go package is a straightforward process. In this article, we created a simple math package, published it on GitHub, and used it in a new project. You can follow these same steps to publish more complex packages or utilities that you might find useful in other projects.

I hope this guide was helpful and that you’re now confident in creating and publishing your own Go packages.

0
Subscribe to my newsletter

Read articles from Sachin Borse directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sachin Borse
Sachin Borse