The Go programming language, also known as Golang, was developed by a team of Google employees in 2007. Its primary objective was to provide developers with a language that could leverage the capabilities of high-performance, networked, multi-core computing chips. Today, Golang boasts a vast community of over a million coders worldwide. One of the key features that sets Go apart is its emphasis on manageable packaging, making it easier for developers to collaborate and share code effectively.
What is the GOPATH?
At the heart of Golang’s package management is the GOPATH, an environment variable that points to the root folder of a Go Workspace. A Go Workspace is a collection of files that includes source code, compiled binaries, external libraries, and cached objects. It serves as the foundation for any Golang development project.
To view the currently configured GOPATH on your system, you can use the following command:
go env GOPATH
Executing this command will display all the Go environment variables on your system. By targeting the GOPATH variable, you can determine its location. The output may resemble the following:
GOPATH="/home/example_user/go"
By default, if you haven’t set your GOPATH environment variable, it will be located in $HOME/go on Linux and macOS, and %USERPROFILE%go on Windows.
Go Workspace Layout
A Go Workspace follows a specific directory hierarchy. Let’s take a closer look at its structure:
$GOPATH/src/: This subdirectory contains Go source files. It serves as the storage location for your development code. Within thesrcdirectory, you can have multiple Go source repositories or projects.$GOPATH/bin/: This directory stores the compiled executable binaries built by the Go engine.
Go developers typically use a single Go Workspace to store all their Go source code. It is also common to organize the code within the $GOPATH/src/ directory based on the source repository. This convention is recommended because it assumes that the Go code will eventually be stored in a remote repository, such as GitHub, allowing for easy collaboration and sharing. Here’s an example of a directory hierarchy that follows this convention:
$GOPATH/ bin/ hello_world # command executable src/ github.com/ username/ go_example/ .git/ # Git repository metadata hello_world/ # command source hello_world.go # command source
Go Workspace Configuration: Setting the GOPATH
Setting the GOPATH is not necessary unless you want to use a location different from the default. As mentioned earlier, the default location for the GOPATH is $HOME/go on Linux systems. To set a custom GOPATH, you can use the following command:
export GOPATH=/home/example_user/a_new_workspace
Replace /home/example_user/a_new_workspace with the desired directory path.
To ensure you don’t have to enter the full path to a Go executable each time you run your Go apps in development, it’s recommended to add the $GOPATH/bin directory to your system’s PATH environment variable. This can be achieved by executing the following command:
export PATH=$PATH:$(go envGOPATH)/bin
Note: The following examples assume that you are using the default location for your GOPATH ( /home/username/go). If the go directory doesn’t exist in your home folder, you can create it by running the command mkdir ~/go.
To start writing your first Go program, you need to create the bin and src directories in your Go Workspace. You can do this by executing the following command:
mkdir $GOPATH/{bin,src}
In the next sections, we will guide you through the process of creating a Hello, World! Go program, following Go conventions for directory hierarchy and organization.
Creating a Hello, World! Go Program
To create a Hello, World! Go program, you should follow the Go conventions and store the program as if it were pushed to a remote version control repository. Let’s continue building the directory hierarchy for your Go Workspace by creating the necessary directories:
mkdir -p $GOPATH/src/github.com/username/go_example/hello_world/
Next, using your preferred text editor, create a new file named hello_world.go in the directory $GOPATH/src/github.com/username/go_example/hello_world/. Add the following content to the file:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
To compile and install the hello_world program, navigate to your home directory ( cd ~ ) and run the following command:
go install github.com/username/go_example/hello_world
Upon executing the above command, Go will create an executable binary named hello_world in your $GOPATH/bin directory. Since the $GOPATH/bin directory is added to your system’s PATH, you can now invoke the hello_world executable from anywhere without providing the full path. Simply run the following command:
hello_world
The output of your Go program will be:
Hello, World!
Conclusion
Understanding how the GOPATH is used in Go is crucial for getting started with Go programming. The GOPATH points to the location of your Go Workspace, which contains all the necessary files for a Go development project. By default, the GOPATH is located at /home/username/go on Linux systems. However, you can customize its location by setting the GOPATH environment variable.
Following Go program conventions for directory hierarchy and organization helps you keep your Go code shareable with collaborators and users. It is recommended to organize your code within the $GOPATH/src/ directory based on source repositories.
Now that you have a good understanding of the GOPATH and how to use it, you can proceed to explore Go Packages in our “Getting Started with Go Packages” guide. This guide will provide you with insights on organizing, packaging, and distributing your Go programs efficiently.
At Shape.host, we provide reliable and scalable Cloud VPS solutions that empower businesses with efficient and secure hosting. If you’re looking for a dependable hosting provider, consider Shape.host for all your cloud hosting needs.