Select Page

Introduction

Go is a general purpose programming language aimed at scalability. It is primarily used for system programming and is influenced by the C programming language. Go is a good programming language of choice for beginner programs due to its ease of use, and the minimal effort required to fix bugs.

Go has a handful of command line tools that can be used along for various purposes, one of which is the goimports command. Goimports command is crucial when compiling many third-party programs written in Go. It is used to manage the import lines of code in a Go program, which adds missing imports required by the program and removes the unrequired ones.

Some users have faced issues while trying to run to command, or at times when compiling some other program based on Go. The error message outputted by the terminal looks something like this:

$ goimports: command not found

If this is the same exact situation you’re facing, then this article should be the only guide you’ll require to rip off this error message away from your terminal screen.


Install the go package

The first thing you need to do in order for the goimports command is to install the command in your machine. But there is no direct way you could do so, because in order for it to be installed, you first need the go language package. Afterwards, you can use the go command to install the goimports sub-command. 

Before anything else, to install the go package in your machine, you need to use your Linux distribution’s package manager. Problem is that the package manager is different for different distributions. Some of the commands required to install the mackage in the most commonly used Linux distributions are listed below:

Arch Linux

$ sudo pacman -Syu go

Ubuntu

$ sudo apt-get update
$ sudo apt-get install go

Debian

$ sudo apt-get update
$ sudo apt-get install go

Fedora

$ sudo yum makecache
$ sudo yum install go

CentOS

$ sudo dnf makecache
$ sudo dnf install go

RedHat

$ sudo dnf makecache
$ sudo dnf install go

Kali Linux

$ sudo apt-get update
$ sudo apt-get install go

In all the mentioned distributions other than Arch Linux, the first line of command is required to update your system’s repository, while the second line of command installs the actual go package.

Once you’re done installing the package, you might as well want to make sure that the package has properly been installed and can be accessed via any directory in your machine. To check this up, run the following command:

$ go version

If the command returns a proper output with no error message, then you’ve successfully installed the go package. 

But don’t hope that you’re done yet, because we’re still left to install the actual command, which is goimports. Now using the go command, you can install it, but before we do so, there are a few things I need to cover up for other fellas out there. If you’re sure that you’ve successfully installed the package, you can directly head to the “Install goimports command using go” section of this article. 


Install go from source binary

For my fellow comrades who couldn’t install the go package using a package manager, don’t be disheartened as we still have another option for us left to install it, which is by using the source binary. 

The first thing you need to do is download the tarball from Go’s official download page. To do so, we’ll use the wget command:

$ wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz

When the download is over, head to the directory where you have downloaded the package, and now extract the tarball using the tar command in your machine:

$ sudo tar -C /usr/local/ -xzf go1.13.5.linux-amd64.tar.gz

What the above command will do is that not only the tarball will get extracted, it’ll also be directly sent to the /usr/local directory, which will allow you to execute the command from any directory you might be in. This is extremely crucial in case you need to command when building a package with the make command, as otherwise make will not be able to trace for the executable files for go

But there’s a slight, little problem. Some system’s don’t have the /usr/local directory listed in their global PATH variables, which you need to fix if you’re installing go from the source binary.


Fix your system’s path variable

In order to add the path to the go executable in our system’s PATH variable, we will have to use the export command. Using the nano command line editor, we’ll include the command we need inside a file called .profile which should be located in our home directory. If it doesn’t it’ll automatically be created:

$ sudo nano ~/.profile

Once you’re inside the file, type out the following line of code:

export PATH=$PATH:/usr/local/go/bin

Now save and exit the file by first pressing Ctrl + O, followed by Enter and another Ctrl + X. Lastly, restart your terminal and run the following command to check whether the procedure was successful or not:

$ go version

If you get a proper output, congratulations. you’ve successfully installed go and are now ready to install the goimports command.


Install goimports command using go

Don’t worry if you’re tired after installing go, because installing goimports will take a single line of command to be executed in the terminal, which is the one mentioned below:

$ go install golang.org/x/tools/cmd/goimports@latest

Using this command, you’ll directly be able to install the goimports command. After all this mess, I’m hopeful you’ve successfully installed the goimports command, and are now dancing in peace!