Sunday, January 31, 2010

Gospecify: Basic setup of the project's structure

During the last two days I played a bit with GoSpecify by Samuel Tesla, a BDD framework for the Go Language. I'm coming from 4 years of intense Ruby development and, you know, I could not live without a good BDD framework. After watching Samuel Google TechTalk I decided to explore BDD under Go. The first problem I faced and solved was with basic files and folders setup. In this post I'll describe how I managed the issue using project structure and makefiles taken from gospecify project itself.

First of all, let's create the project folder
mkdir fib
Yeah, you guessed right, we'll deal with yet another Fibonacci sequence implementation ;)

Now, move in fib folder and create a basic project structure
cd fib
mkdir src
mkdir spec
As you guess src will contain source files and spec will contain the specs. Now let's go creating a Makefile in the project base path. Create an empty Makefile using your favourite text editor and copy/paste the code below:
These are the up-level's rules with which we will interact. Names are self-explanatory however I'll describe the corresponding actions below:
make clean
Clean up project folder removing intermediate and backup files

make test
Run the specs

make format
Format the source files using gofmt

make package
Build the package
As you can see, some of the rules above assume there is another Makefile inside src/ folder. Thus, move on src/ and create the following Makefile:
Note that we have to set the PACKAGE variable with the name of the package (fib in this case). Also, note that the testpackage rule in the Makefile above will create a package named test$(PACKAGE).a. So, in our case, we will get src/testfib.a. We will import this package in our test code to actually run the specs.

At this point, the basic project structure and makefiles are done. Now, we can populate src/ folder with an empty source file (with just the basic package clause within):
cd src
echo "package fib" > fib.go
Now, following BDD convention (test-first approach), let's write our test. Enter in spec/ folder and create spec/fib_spec.go with the code below:
The spec above tell us how we expect the system behaves before actually implement it. In particular, we setup two seed values N0 and N1 and then we query fib about the next value in the sequence using fib.Next() method. For the first five calls we expect the sequence: 1, 1, 2, 3, 5.

If we try to run the specs at this point we get an error of course (we haven't an implementation yet):
$ make
...
fib_spec.go:9: undefined: testfib.Fib
...
Open the text editor and paste in src/fib.go the code below:
If you're new to Go, take your time reading the code. Also consider that I'm still learning this language from the ground up so this could not be the best idiomatic implementation (waiting for suggestions in this case). BTW, for the purpose of this post, our code behave as we expect. In fact, running the specs
$ make test
.
Passing: 1 Failing: 0 Pending: 0 Errors: 0
they all pass.

Well, that's all for now! You can browse the source files used in this post on github.

Tuesday, January 26, 2010

Installing Go: go simple!

So finally I decided to run a blog. And yes, you're reading my first blog post ever!

This afternoon I decided to give a whirl to Go Programming Language. I don't know yet so much about it but I'm intrigued by some concepts like goroutines and efficient GC. Moreover, I was amazed by the very first impact with the language: the building process is foolproof (at least on my Ubuntu box) so I decided to document the process as a tribute to this neatness. It's an example of how things should be done in computer programming. Moreover, it's a good starting point for my first blog post ever, isn't it?

Assuming you're on a debian based system you need - first of all - to install the toolchain to building the language:

sudo apt-get install bison gcc libc6-dev ed gawk make

And, if you decide to clone the mercurial repository, you need mercurial of course!

sudo apt-get install mercurial

Now that the prerequisites are satisfied let's configure our build. Here's the neatness of the whole process. You setup a bunch of environment variables and you're done! So for example, assuming that we wish to:
  • Clone Go repository in $HOME/go

  • Build Go for a linux distribution

  • Build Go for a i386 architecture
all we need is to export the following variables:
export GOROOT=$HOME/go
export GOARCH=386
export GOOS=linux
And we can save them in .bashrc if we want a persistent configuration.

The installation process assumes that $HOME/bin folder exists and it places binary files in it. Let's meet this last prerequisite:
mkdir $HOME/bin
You can change binaries' folder setting the $GOBIN variable. In order to run the executables, don't forget to update your $PATH variable appending $HOME/bin.
export PATH=$PATH:$HOME/bin
Well, now let's go cloning the repo and building the whole stuff:
hg clone -r release https://go.googlecode.com/hg/ $GOROOT
cd go/src
./all.bash
At this point the installation process assumes that $HOME/bin folder exists and it places binary files in it (you can change folder name setting the $GOBIN variable). And finally let's test our build starting the compiler:
$ 8g
flags:
-I DIR search for packages in DIR
-d print declarations
-e no limit on number of errors printed
-f print stack frame structure
-h panic on an error
-o file specify output file
-S print the assembly language
-w print the parse tree after typing
-x print lex tokens
And that's all, no autogen.sh, no configure scripts to run! And is even more beautiful because we are actually cross-compiling! That is, we could produce builds for any other supported OS/ARCHs by simply changing the values of the environment variables above! Can't wait to test on my shining new beagleboard when it will arrive! :D

For further readings see: