How to Create Your Own Package in Golang?


Golang is an open-source programming language that is widely used for building robust, scalable, and efficient applications. One of the key features of Golang is its package system, which allows developers to write reusable and modular code. In this article, we will discuss how to create your own package in Golang.

What is a Package in Golang?

A package in Golang is a collection of related functions, types, and variables that are organized together to provide a specific functionality. Packages can be reused in different projects, making them a powerful tool for developing modular and maintainable code.

Golang provides a number of built-in packages for common tasks such as file I/O, networking, and cryptography. However, you can also create your own custom packages to encapsulate your code and make it reusable.

Creating a Package in Golang

Creating your own package in Golang is a simple process that involves the following steps −

  • Create a new directory for your package

  • Add your source code files to the directory

  • Create a file called go.mod in the directory to define the module name

  • Define the package name in your source code files

  • Build your package

Let's take a closer look at each of these steps.

Step 1: Create a New Directory

The first step in creating a package is to create a new directory to hold your package code. This directory should be named after your package and should be located within your GOPATH directory.

For example, if you wanted to create a package named "mathutils", you would create a new directory called "mathutils" within your GOPATH directory.

$ mkdir $GOPATH/src/mathutils

Step 2: Add Your Source Code Files

Next, you need to add your source code files to the directory you just created. The files should be named after the package they define and should have the extension .go.

For example, if you wanted to create a package named "mathutils" with a function to calculate the average of a slice of numbers, you would create a file called average.go in the mathutils directory and add the following code −

package mathutils

func Average(numbers []float64) float64 {
   sum := 0.0
   for _, number := range numbers {
      sum += number
   }
   return sum / float64(len(numbers))
}

Step 3: Create the go.mod File

The go.mod file is used to define the module name and version for your package. To create a go.mod file, navigate to the directory where you created your package and run the following command −

$ go mod init mathutils

This will create a new go.mod file in your package directory with the following contents −

module mathutils

The module line specifies the module name, while the go line specifies the minimum version of Golang required by your package.

Step 4: Define the Package Name

In your source code files, you need to define the package name using the package keyword followed by the name of your package.

For example, in the average.go file, we defined the package name as mathutils −

package mathutils

func Average(numbers []float64) float64 {
   // ...
}

Step 5: Build Your Package

Finally, you need to build your package using the go build command. This will compile your source code files into a single package file that can be imported into other Golang projects.

To build the package, open the terminal and navigate to the root directory of your package. Run the following command to build the package −

go build

This will create an executable file with the same name as your package in the current directory.

If you want to build the package in a specific directory, you can specify the output directory using the -o flag followed by the desired output directory −

go build -o /path/to/output/directory

Step 6: Test Your Package

Before publishing your package, it's important to test it thoroughly to ensure it's working as expected.

To test your package, you can create a separate test file within your package directory. The test file should have the suffix _test.go and contain test functions that start with the word Test. For example, if your package is named mypackage, your test file should be named mypackage_test.go.

Here's an example of a test function −

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

To run the tests, navigate to the root directory of your package in the terminal and run the following command −

go test

This will run all the tests in your package.

Step 7: Publish Your Package

Once you're confident that your package is working as expected, you can publish it to a repository for others to use. There are several popular repositories for Go packages, including −

  • Go Modules

  • GitHub

  • Bitbucket

  • GitLab

To publish your package, you can create a repository on one of these platforms and upload your package files. You can also use the go get command to download your package from a repository −

go get github.com/your-username/your-package

This will download your package and all its dependencies into your $GOPATH/src directory.

Conclusion

Creating your own package in Go is easy and can help you modularize your code for better maintainability and reusability. By following the steps outlined in this article, you can create your own package, build it, test it, and publish it for others to use.

Updated on: 05-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements