Install Third Party Packages

Install Third Party Packages

In this tutorial, we will discuss how do we install third party packages in the Go language.

Install Third Party Packages

We can download and install third-party Go packages by using go get command. The go get command will fetch the packages from the source repository and put the packages on the GOPATH location.

For example,

go get gopkg.in/mgo.v2

The above command in the terminal will install “mgo”, a third-party Go driver package for MongoDB, into your GOPATH, which can be used across the projects put on the GOPATH directory.

After installing the mgo, put the import statement in your programs for reusing the code, as shown below:

import (        
        "gopkg.in/mgo.v2" 
        "gopkg.in/mgo.v2/bson"       
)

The MongoDB driver, mgo, provides two packages that we have imported in the above import statement.

When using go get to install third-party packages, it is common for a package to be referenced by its canonical path. That path can also be a path to a public project hosted in a code repository such as GitHub.

As such, if you want to import the flect package, you would use the full canonical path:

$ go get github.com/gobuffalo/flect

The go get tool will find the package on GitHub in this case and install it into your $GOPATH. For this example the code would be installed in this directory:

$GOPATH/src/github.com/gobuffalo/flect

The original authors are often updating packages to address bugs or add new features. When this happens, you may want to use the latest version of that package to take advantage of the new features or resolved bugs.

To update a package, you can use the -u flag with the go get command:

$ go get -u github.com/gobuffalo/flect

This command will also have Go install the package if it is not found locally. If it is already installed, Go will attempt to update the package to the latest version.

The go get command always retrieves the latest version of the package available.

That’s all about the Install Third Party Packages in Go language. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Go language.!!

Install Third Party Packages
Scroll to top