Skip to content

Instantly share code, notes, and snippets.

@whitehorse0
Last active June 18, 2023 16:32
Show Gist options
  • Save whitehorse0/d45695ca4990368e20eafef5d5ea3fcf to your computer and use it in GitHub Desktop.
Save whitehorse0/d45695ca4990368e20eafef5d5ea3fcf to your computer and use it in GitHub Desktop.

Setup Golang Workspace (My ZSH Configuration)

Download & Install

https://go.dev/dl/go1.19.10.darwin-arm64.pkg

Update your shell profile

Go expects the path of the go workspace as the $GOPATH environment variable. So you need to set up two environments to add the following two lines to your ~/.zshrc or ~/.bashrc or ~/.profile:

export GOPATH=$HOME/Project/Go

Also added the $GOPATH/bin to my system path so that I can use the commands installed from “go install” or “go get”.

export PATH=$GOPATH/bin:$PATH

You can set the environment variable GOPATH to any directory you like. If you have larger projects, it's probably a good idea to create a different GOPATH for each of them. I would recommend this approach especially for the deployment, so that updating a library for project A doesn't break project B which might require an earlier version of the very same library.

And do not forget to reload your change .zshrc afterward

$ source ~/.zshrc

Then create your the workspace directories:

$ mkdir -p $GOPATH $GOPATH/{src,pkg,bin}

Test your environment

$ go env

For example, I created a directory named “github.com” under “src” and then created “whitehorse0” under that. Now my projects would reside in: ~/Project/Go/src/github.com/whitehorse0 directory.

I can now treat ~/Project/Go/src/github.com/whitehorse0 directory as my personal workspace. So, I added these to my .zshrc for convenience:

export GOWORKSPACE=$GOPATH/src/github.com/whitehorse0

Once I got this done, I can actually do this when creating a new project:

$ cd $GOWORKSPACE 
$ take my_first_project
$ go mod init my_first_project

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment