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

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 that is 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
Packages are often being updated by the original authors 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 bug.
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.