Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

In Linux, Go is a powerful and open-source programming language developed by Google to simplify modern software development. While working on Go projects, we often need to configure the GOPATH environment variable to make sure that the project’s code runs smoothly within the specified directory.

In this tutorial, we’ll discuss the procedure for setting up the GOPATH environment variable on Linux. Also, we’ll see some important usage of Go modules for managing the project’s dependency.

2. Exporting Variables via Command-Line

Before proceeding, let’s quickly look at exporting variables via the command line interface. For this purpose, we can use the export command, which temporarily sets the variable for the current shell session.

For example, to export the GOPATH and PATH environment variables, we can run these commands:

$ export GOPATH=$HOME/go 
$ export PATH=$PATH:$GOPATH/bin

Now, upon echoing the variables, we should get the defined directory paths:

$ echo $GOPATH
/home/henry/go
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/henry/go/bin:/home/henry/go/bin

Please note that once the terminal is closed, the variables will no longer be set.

3. Setting up GOPATH Variable

For Go projects, the GOPATH defines the workspace directory, storing the source code, dependencies, and binaries in the go.mod file. We can edit the .bashrc file to specify the GOPATH variable using the export command. With this setup, the variable will automatically export the GOPATH variable whenever a new terminal is open. In short, this method is useful for setting the variable permanently.

To illustrate, let’s open the .bashrc file using the nano command:

$ nano ~/.bashrc

Then, add these lines at the end of the file to define the GOPATH variable:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Our edited .bashrc file.

Afterward, we save the .bashrc file and reload it using the source command:

$ source ~/.bashrc

Now, we can run the echo command to check the configured GOPATH environment variable:

$ echo $GOPATH
/home/henry/go

As a result, we can see that the GOPATH variable is set to the path /home/henry/go.

4. Using Go Modules

Go Modules were introduced in Go 1.11 and became the default dependency management system in Go 1.13. With this system, dependency management has become much easier and more efficient. Now, instead of setting the GOPATH variable, we can handle project dependencies directly at the module level.

To initialize a module in a project directory, we use the go mod init command:

$ go mod init <module-name>

Here, we replace <module-name> with the project name or repository link.

For instance, to initialize the repository github.com/Mateen0007/Spoon-Knife.git, we can execute this command:

$ go mod init github.com/Mateen0007/Spoon-Knife.git
go: creating new go.mod: module github.com/Mateen0007/Spoon-Knife.git
go: to add module requirements and sums:
	go mod tidy

After initializing, we can add the dependency using this command:

$ go get <dependency>

In the above command, we replace <dependency> with the required dependency.

5. Conclusion

In this article, we’ve learned how to set up the GOPATH environment variable on Linux. We started with temporarily exporting variables via the command line. Then, we showed setting the GOPATH variable permanently in the .bashrc file.

Finally, we’ve also explored the use of Go modules, which is the modern and preferred approach for managing project dependencies.