golang

Go Get Install Package

Go provides us with a standard tool for downloading and installing packages in our projects. You can specify a package URL using the go get command and Go will fetch it and install it.

This article will discuss how to install Packages with the go get command.

The Go Get Command

The go get command allows you to download and install packages and dependencies.

The command downloads and installs the packages with the specified version. It also updates the go.mod file to include the installed packages.

Installing a Package with go get

Install a package using the go get command and ensure you have the compiler installed and added to the path.

You can do so by running the command:

$ go help get

The command above should print out the help information for the get command.

To install a package, start by creating a sample project directory:

$ mkdir test_project

$ cd test_project

Once you have your project setup, run the go mod init command to initialize a go module.

$ go mod init test_project

Create a new file to hold your golang source code in the project directory.

$ touch main.go

Set GOPATH

You need to worry about two significant environment variables when working with Go:

First, the GOROOT environment variable. It is used to define the location of your Go SDK. In most cases, you will not need to modify this value unless you specify different Go versions.

Second, the GOPATH variable. This variable defines the root path of your workspace. By default, the value is set to the go folder in your home directory.

For example, in Linux, the GOPATH is set to:

~/go

In Windows:

%userprofile%go

When installing packages, you pay attention to this directory as it holds all your codebases and the dependencies.

Although you can configure a custom GOPATH directory, we recommend you stick with the default to prevent issues such as permissions.

To get the path of your GOPATH, run the command:

go env $GOPATH

The command above should print the value of the GOPATH variable:

$ go env GOPATH

/home/debian/go

Before installing packages, start by exporting the GOPATH and PATH variables:

$ export GOPATH=$HOME/go

$ export PATH=$PATH:$(go env GOPATH)/bin

Verify the values are updated by running the go env command as shown:

Go Install Package

To install a package, we use the go get command followed by the URL to the package repository.

For example, to install the aurora package:

https://github.com/logrusorgru/aurora

We can run the command as shown:

go get -u github.com/logrusorgru/aurora

The command above will download and install the aurora package in our project. It will also update the go.mod file to include the installed package version.

Go Install Specific Version

In some cases, you may need to install a specific package version. For that, you can specify the version using the @ operator as shown:

$ go get example.com/pkg@v1.2.3

This installs the specified package version.

Importing a Package

Once installed, you can use the package by importing it using the import statement.

For example, in our test_project, we can use the aurora package as shown:

import (
    "fmt"
    . "github.com/logrusorgru/aurora"
)
funcmain() {
    fmt.Println(Bold(Cyan("Welcome to Linuxhint!!!")))
}

In the code above, we import the aurora package and use it to print a bold and cyan-colored text to the terminal.

Running the code should return:

Conclusion

The guide walks you through downloading and installing go packages using the go get command.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list