How to install packages and dependencies in Golang?

I recently visited an old Golang project, and I wanted to install the packages found in my go.mod file. I ran the command in my terminal:

go get

And I got an error message that said:

'go get' is no longer supported outside a module.

It seems that the go get command is no longer supported in the most recent Golang version.

Solution: Use go install

To install the packages in your project, you need to execute:

go install

The result was that it installed all my packages successfully!

go: downloading v1.6.6
go: downloading v2.3.2
go: downloading v1.3.0
go: downloading v1.18.0
go: downloading v0.0.0-20201231184435-2d18734c6014
go: downloading v0.0.0-20161114210144-ceec8f93295a
go: downloading v1.11.4
go: downloading v1.0.1
go: downloading v1.0.0

Bonus: How to remove from Golang project

This one was a bit tricky, because what I initially tried to do was just delete the package from the go.mod file–when I ran go mod tidy it actually added the package back into the file.

Here is a simple two step process to remove installed packages from a Golang project, let’s dive in!

Step 1: Remove all imports of the package

You must first remove the the import call of the package from any of your Golang project files.

Step 2: Run go mod tidy

Once you’re done with removing any lines that import the package, you can now run the following command in your terminal:

go mod tidy

Once that command finishes processing, you can look at your go.mod file and see if the package you desire to remove is gone.

I like to tweet about Golang and post helpful code snippets. Follow me there if you would like some too!