How to Install Package in GoLang

Sheeraz Gul Feb 15, 2024
  1. Install Package in GoLang
  2. Set the GOBIN Environmental Variable
  3. Initialize the go.mod File
  4. Download and Install the Package
How to Install Package in GoLang

This tutorial demonstrates how to install packages in GoLang.

Install Package in GoLang

Installing a required package is very easy using the get command of GoLang. GoLang provides a variety of commands to perform certain tasks, and get is one of them.

The get command will download the package for us, and then we can use the go install command with the named package to install the executable. The syntax for the get and install commands in GoLang are:

go get [-d] [-f] [-t] [-u] [-v] [-insecure] [-fix] [packages]
go install [Package Name]

Where the get command will download the package with its dependencies, and the other parameters are used for the following tasks:

Parameter Description
-d This parameter is used to ask the get command to only download the package and not install it.
-f This parameter is used to force the -u parameter, which ensures not to verify each package downloaded.
-t This parameter asks the get to download the corresponding package required to build the downloaded package.
-u This parameter asks the get to update the package and its dependencies.
-v This parameter is used to enable debugging.
-insecure This parameter allows the get to download the repositories from insecure domains such as HTTP.
-fix This parameter is used to fix the downloaded package.
-packages This parameter specifies the package name to be downloaded.

The install command will be used to install the downloaded executable file from the get command, where the package name is the downloaded package.

Set the GOBIN Environmental Variable

One more important thing is that make sure that GOBIN is set as the Path environmental variable, and to check, run the following command:

go env

GOBIN Environmental Variable

As we can see, the GOBIN path is set, and in case it is not set, use the following command to set the GOBIN path:

set GOBIN=C:\Program Files\Go\bin

Initialize the go.mod File

The get command will not download any package if it cannot find the go.mod file. For the first-time package installation, we need to initialize a go.mod file.

Let’s try to initiate a go.mod file for the package Awesome Go, which is many purposes in the Go language. This package is provided on GitHub and can be found here.

go mod init [Package Name]

Another important point is to use the command prompt as administrator mode; otherwise, it can throw the Access Denied error. And if the go.mod file already exists, then we don’t need to do it again; the output for the above command is:

Go Mod File

Download and Install the Package

Once the GOBIN path environmental variable is set and the go.mod file is created, which are one-time steps, we can now easily download and install packages.

To download the packages from GitHub, you must also ensure that you have already installed git in your system. If it is not installed, download and install it from here.

Go to the src folder using the command prompt, which is the root directory of our project; in our case, it is C:\Program Files\Go\src, also the go.mod file is located in this folder. Let’s use the get command to install the Logrus package:

go get "github.com/avelino/awesome-go"

The output for the above command is:

Go Get Command

Once the package is downloaded using the get command, we can install it using the install command. It should also be mentioned that using the install command is unnecessary for every package.

See the command:

go install github.com/avelino/awesome-go@latest

The above command will install the package, and we can use it afterward. The output for this command is:

Go Install Command

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Go Package

Related Article - Go Installation