DEV Community

Fakorede Abiola
Fakorede Abiola

Posted on • Originally published at fabcodes.hashnode.dev on

What the $GOPATH is about

Introduction

I just recently started learning the GO programming language. While I have realized that keeping notes of what I am learning along the way is crucial to the learning process, I also decided to as well share some of my notes in the form of short articles revolving around everything I have learnt.

I almost skipped knowing what the $GOPATH is about all together as it wasn't discussed properly in the first resource I tried learning GO with. But from what I have gathered, I realize it is important to understand what the $GOPATH is and understand how it works.

What is $GOPATH?

I'll be answering this in a bullet point format to make it easier to digest. Here are some of the points I gathered:

  • The $GOPATH is an environment variable. It shows a location to a physical directory on a machine which has GO set up.

  • The $GOPATH directory is automatically created under the users folder when the Visual Studio Code GO extension is installed so we do not need to create it.

  • By default, GO assumes that the folder $GOPATH shows is in your users folder. So we do not need to set it.

  • To know what your $GOPATH is, use the command below in your terminal:

$ go env GOPATH

It is usually in the form Users/{user}/go

  • The directory $GOPATH points to is also referred to as a Workspace. It also shows GO tools where it can find GO source code files and other related stuffs like compiled packages, executable binaries and so on. By convention, every GO project uses this folder.

  • Inside this directory are three folders: bin, pkg, and src. The src folder contains GO source-code files and this is where we keep our GO projects.

  • When we download a GO project, it is stored under the $GOPATH.

  • An executable GO program needs its own directory which we create under the $GOPATH/src folder. For example:

$GOPATH/src/helloworld/main.go
  • If you have a GitHub account, it is better to put your project in a github.com folder under the src directory i.e.
$GOPATH/src/github.com/{your-username}/helloworld/main.go
  • Since we will be creating our projects under the $GOPATH/src folder, to open up the folder in VS Code using the terminal, if you're on MAC or have git bash installed on Windows, type:
$ code ~/go/src

Conclusion

Looking back, I realize how vital understanding what the $GOPATH is as a GO developer and how important its concept is in developing GO programs and packages. It is in fact a convention in the GO community and I would definitely urge anyone new to the language to endeavour to understand what it is.

Other Articles 📖

Code Samples - https://github.com/Fakorede/Learning-Golang

Top comments (0)