Skip to content

Instantly share code, notes, and snippets.

@sathiyaseelan
Last active April 1, 2024 18:51
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sathiyaseelan/529695891e290991573d278a56180535 to your computer and use it in GitHub Desktop.
Save sathiyaseelan/529695891e290991573d278a56180535 to your computer and use it in GitHub Desktop.
Basics to setup a golang project repo in your local

Simple Guide to setup Golang in your Mac and clone a repo.

Setup Go and workspace

Type go in terminal, to verify the installation.

  • Create a Go workspace and set GO PATH
mkdir -p $HOME/go
export GOPATH="$HOME/go"

# Folder contains your golang source codes
mkdir -p $GOPATH/src

# Folder contains the binaries when you install an go based executable
mkdir -p $GOPATH/bin

# Folder contains the Go packages you install
mkdir -p $GOPATH/pkg

# Folder contains the Github Source code for the repos you cloned
mkdir -p $GOPATH/src/github.com

All your imports will be resolved from this GO PATH only

For more info: https://golang.org/cmd/go/#hdr-GOPATH_environment_variable

Cloning a go project repo.

The folder structure to maintain the local repo is $GOPATH/src/github.com//

For this document, let's assume you're cloning the below repo. https://github.com/keratin/authn-server

mkdir -p $GOPATH/src/github.com/keratin/authn-server
cd $GOPATH/src/github.com/keratin/authn-server

git clone git@github.com:keratin/authn-server.git

Installing Go packages

To install a go package, you can use go get command.

go get github.com/benbjohnson/ego/cmd/ego

go get github.com/airbrake/gobrake

Note: Depends on the package, it may install a binary under $GOPATH/bin or a package in $GOPATH/pkg

Using Glide

Glide is a package manager for go-lang. Some projects use Glide for maintaining the Go packages. This is similar bundler in rails or maven in Java.

To install Glide

brew install glide

To install the pacakges in glide.yaml

glide install
@pablodz
Copy link

pablodz commented Jul 28, 2021

Nice

@lmf-git
Copy link

lmf-git commented Oct 4, 2021

Or you can just use:

go mod tidy

...? 🤔

@aakashbajaj
Copy link

Worked for me 💯

@umairishaq
Copy link

When cloning the keratin/authn-server repo as suggested above, the code is located a level deeper than intended at: $GOPATH/src/github.com/keratin/authn-server/authn-server. For the intended location I suggest,

mkdir -p $GOPATH/src/github.com/keratin
cd $GOPATH/src/github.com/keratin

git clone git@github.com:keratin/authn-server.git

@azmuthu
Copy link

azmuthu commented Jan 26, 2023

I cloned a github project and I want to make changes to it locally to test the functionality. How do I make go use the local copy of the public project instead of the github one?

@prasunparijat
Copy link

prasunparijat commented Sep 1, 2023

While being on the master/main branch that you have freshly cloned into you local system, you can create a new local branch using git checkout -b "your_branch_name". This will create a local copy from the master/main branch. But I believe that you already might know this. It is advisable to follow a naming convention familiar to you while creating the new local branch - "fix/" or "feature/".
Please be a bit more clear on your query, as to if you need the command to just run the code locally / resolve dependencies etc,.

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