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
$GOPATHis an environment variable. It shows a location to a physical directory on a machine which has GO set up.The
$GOPATHdirectory is automatically created under theusersfolder when the Visual Studio Code GO extension is installed so we do not need to create it.By default, GO assumes that the folder
$GOPATHshows is in yourusersfolder. So we do not need to set it.To know what your
$GOPATHis, use the command below in your terminal:
$ go env GOPATH
It is usually in the form Users/{user}/go
The directory
$GOPATHpoints 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, andsrc. Thesrcfolder 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/srcfolder. For example:
$GOPATH/src/helloworld/main.go
- If you have a GitHub account, it is better to put your project in a
github.comfolder under thesrcdirectory i.e.
$GOPATH/src/github.com/{your-username}/helloworld/main.go
- Since we will be creating our projects under the
$GOPATH/srcfolder, 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.
Top comments (0)