Thursday, December 3, 2015

Notes on "Go" Programming, Learned From Diving-in to a Blockchain Project

I decided to start using the “go” programming language a while ago, but I never had a project that could introduce me to it until recently.  There were a few things about Go that I knew already, and they seemed appealing...

  • It was created by a legit, old-school computing power-trio (Thompson, Pike, Griesemer)
  • Compiled, but the compiler is extremely fast
  • Imperative
  • Statically typed
  • Supports pointers
  • Supports user-defined data structures
  • Not really object-oriented
  • Has a lot of built-in features to support multithreading

Basically, it’s what Ada should have been but never was.  There are also a bunch of things I learned about Go while I worked on my first project:

  • Go has built-in dependency support
  • Go has built-in unit test support
  • There are no makefiles, but you do need to adhere to a particular directory structuring model
  • It is probably easier to come downstream to Go from Python than upstream from C.
  • Go tries to protect the coder from pointers, without eliminating pointers — this makes me crazy.
  • There’s an Old—World attitude about the syntax formatting: technically you have a lot of freedom with the syntax, but in terms of practical culture, you do not.  Go will sneakily adapt your syntax to its own ideal.
  • Go’s write-test-deploy model is faster than pretty much anything else’s is — including any LISP.

The whole reason I embarked on learning some Go is because there was a minimum viable blockchain project I found, implemented in Go.  I wanted to play around with it, and test it.  I’m not even going to bother showing code snips, because they are freely available.  Here’s what an observer should know about getting started with Go.

1. Install Go.  You probably need Mac or Linux.  If you’re reading this, you probably already have Mac or Linux.

2. Edit your .profile file to include:
export GOPATH=“$HOME/go”

Go has a strict directory structure model.  This took me a while to figure out.  Go doesn’t appreciate creativity, here.  You need to put all of your source in another directory called $GOPATH/src.  By “all source” I mean every Go source code project you ever create or download.  Yes, you can set up nested directories.  In fact, Go sort-of fits right in to Git.  If you’re downloading Git repos into $GOPATH/src, everything just works.

3. Download into $GOPATH/src

4. Run a Go command to download dependencies
$ cd $GOPATH/src/
$ go get

5. izqui/blockchain had some bugs.  Once you fix them you can run the following command, and expect the following output (or similar).  Go supports inline testing, which is really handy.  You just name test codefiles with “_test.go” at the end.  Yeah, pretty easy.
$ cd core
$ go test -v
=== RUN   TestMerkellHash
--- PASS: TestMerkellHash (0.14s)
=== RUN   TestBlockDiff
--- PASS: TestBlockDiff (0.07s)
=== RUN   TestKeyGeneration
--- PASS: TestKeyGeneration (0.00s)
=== RUN   TestKeySigning
--- PASS: TestKeySigning (0.03s)
=== RUN   TestMessageMarshalling
--- PASS: TestMessageMarshalling (0.00s)
=== RUN   TestPow
--- PASS: TestPow (0.00s)
=== RUN   TestTransactionMarshalling
--- PASS: TestTransactionMarshalling (0.13s)
=== RUN   TestTransactionVerification
--- PASS: TestTransactionVerification (0.01s)
=== RUN   TestIncorrectTransactionPOWVerification
--- PASS: TestIncorrectTransactionPOWVerification (0.00s)
=== RUN   TestIncorrectTransactionSignatureVerification
--- PASS: TestIncorrectTransactionSignatureVerification (0.01s)
ok 0.413s

6. You can deploy the command line client to this program equally as easily.
$ cd ../cli
$ go install
$ cd $GOPATH/bin
$ ./cli

So, that’s my first experience with Go.  I expect I’ll use it as a replacement for anything between C and Python (but not replacing C or Python for me).  It is really quite streamlined.