Skip to main content

Command Palette

Search for a command to run...

What the $GOPATH is about

Updated
โ€ข0 min read

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.

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

Golang: My Journey to Mastery

Part 1 of 6

I recently started learning๐Ÿ“– the GO programming language. In this series, I will be sharing๐Ÿ‘จ๐Ÿฝโ€๐Ÿซ most of the stuff I'm learning on this journey to mastery.๐Ÿค“ I'm also open to learning from readers.

Up next

The Classic Hello World Program in GO

Introduction To write your first code in any programming language, it has become a norm to start with a simple hello world program. In this post, I will be showing how to write a program in GO and explain all the parts of the program. Lets begin!๐Ÿ‘๐Ÿผ...

More from this blog

Welcome to Fabcodes | A technical blog run by Fakorede Abiola

9 posts

Hello, I am Fakorede Abiola. An experienced Backend Developer with keen interest in Laravel, Node.js & Golang. I also do frontend development with Vue.js and React.js. Currently learning Dart/Flutter.