Import a Third Party Package in Golang

Learn via video courses
Topics Covered

Overview

The standard library of Go offers a rich set of packages and tools. However, we can also extend the functionalities of our application using third-party packages.

Go provides a very intuitive way to use third-party packages.

Introduction to Installing Third-Party Package from Golang

Go has a large selection of standard packages to choose from, however, they cannot handle all programming requirements. Golang offers commands like go-get that make utilizing third-party packages quite simple and support powerful package management systems. In this article, we will learn more about go get and packages.

What is the Go Get-Command?

We can download and install third-party packages by using the go get command. The go get command will fetch all the packages from the source repository and put the packages in the GOPATH location. These packages can be then utilized and used in our code.

How to Use Go Get-Command to Fetch Packages?

You can import a package by executing the below go command.

The above command will fetch the third-party package from the repo and store it in GOPATH.

Implementation of the Go Get-Command

  1. Execute the below command to get the package available at GOPATH.
  1. Writing a Go file that uses the below package.

Output

Try on Playground

Output of the Fetched Packages

The go get tool will find the package on GitHub for the examples we implemented. The downloaded packages will be stored at the below location.

If you want to update a package use the -u flag with the go get command.

Conclusion

  • Learnt about third-party packages in Golang.
  • Implemented examples in Golang which uses external packages.