Sharing code

Adding a Go project to a public Git repo allows others to download and install your code.

Setting up with git

From the base folder ($GOPATH/src/ where our Go code resides, execute:

> git init
> git add --all
> git commit -a -m "Initial Commit"

This initializes our git repo locally and commits all our changes to it.

Initialize Git repo remotely

These steps depend on the chosen remote Git repo. For our case, we initialized a new remote repo using Atlassian’s Bitbucket, which was as simple as clicking “Create repo” in the web UI.

Set remote origin and push

Again from a Terminal at $GOPATH/src/, execute:

> git remote add origin
> git push -u origin master

Voila! Your Go code is now tracked in source control.

Demonstrate shared code

Anyone with access to your git repo (which means the whole world if it’s a public repo) can download / install / execute your code by running:

# Move the code to a backup location
> cd $GOPATH/src/
> mv go-public go-public-backup
> ls

# Demonstrate ability to fetch library code with goget
> go get
> ls
go-public go-public-backup
# ^ Notice we downloaded the 'go-public' repo!
# Test the code to show it tests work!
> go test
ok    0.001s

# Clean up any old copy we had of 'hello' to demonstrate executable code
> rm $GOPATH/bin/hello
> hello
bash: hello: command not found...
> go install
> hello

# Don't forget to restore the backup
> cd $GOPATH/src/
> rm -rf go-public
> ls
> mv go-public-backup go-public
◀   Testing Go Code Building a simple webapp   ▶