Open In App

How to Install Go Project Dependencies?

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

We all know that project management is the most essential key for the setup of any project and in the same way, installing dependencies is also one of the important parts of project management.

For installing the go project dependencies, we use go get command, which automatically updates the go.mod file. You can also install go project dependencies by following a few steps, but remember as the package is still not used anywhere in the project it will be marked as indirect. And once you have configured the project for the use of go modules then you can introduce some more new dependencies into your codebase.

Below are all the steps that one needs to follow in order to install go dependencies.

Step 1: Create Workspace

  •  First of all, we are going to create a workspace for the project, which will involve the below-mentioned steps:
  •  Firstly we need to create a workspace or project folder and then inside that workspace, we need to create three subdirectories.
mkdir myproject
cd myproject
  • Names of the three subdirectories will be BIN, SRC, and PKG respectively.
mkdir bin
mkdir src
mkdir pkg

Create Workspace for Go projects

  • Now after doing all this the structure of our workspace is ready and now next we need to export the location of this workspace so that we can use the Go path variable.
pwd
export GOPATH=$HOME/golang_tutor/myproject
  • Now we need to export the BIN directory which we created inside the workspace so that we can use the path variable.
export PATH=$GOPATH/bin:$PATH

Install GO Project Dependencies:

Now our workspace is ready to work and here are some simple steps to install go project dependencies into your workspace.

Step 2: Choose the package you want to install using the go get command. Suppose, you are installing one package named mux from github.com using the go get command. You can also use any other package from any other medium also.

go get github.com/gorilla/mux
ls

Install GO Project Dependencies

Now in your workspace, you can see your source file inside src folder and inside pkg folder you can see the package object. And the bin directory is empty.

ls src/
ls pkg/
ls bin/

So this is all that it takes to install a dependency in a go project using go get command.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads