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/bitbucket.org/theandruu/go-public) 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/bitbucket.org/theandruu/go-public, execute:

> git remote add origin https://bitbucket.org/TheAndruu/go-public.git
> 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/bitbucket.org/theandruu
> mv go-public go-public-backup
> ls
go-public-backup

# Demonstrate ability to fetch library code with goget
> go get bitbucket.org/theandruu/go-public/learning/stringutil
> ls
go-public go-public-backup
# ^ Notice we downloaded the 'go-public' repo!
# Test the code to show it tests work!
> go test bitbucket.org/theandruu/go-public/learning/stringutil
ok      bitbucket.org/theandruu/go-public/learning/stringutil    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 bitbucket.org/theandruu/go-public/learning/hello
> hello
The-lazy-fox-jumped-over-the-brown-dog.

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