# First steps with Go


I joined the go-nuts mailing list a few days ago and it really feels
good to receive 200+ emails per day again. Just like in the good old
days before good spam filtering (and anti-spam laws).

I also re-watched the presentation Rob Pike gave for Google Tech Talks
on youtube.com. In there he presented the following program `chain.go`:
(Formatted with `gofmt` as it should)

    package main

    import (
	    "flag";
	    "fmt";
    )

    var ngoroutine = flag.Int("n", 100000, "how may")

    func f(left, right chan int)    { left <- 1+<-right }

    func main() {
	    flag.Parse();
	    leftmost := make(chan int);

	    var left, right chan int = nil, leftmost;
	    for i := 0; i < *ngoroutine; i++ {
		    left, right = right, make(chan int);
		    go f(left, right);
	    }
	    right <- 0;             // bang!
	    x := <-leftmost;        // wait for completion
	    fmt.Println(x);         // 100000
    }

In this short program we make a chain of 100000 *goroutines* which are
connected to each other. Each one adds 1 to the value it gets from its
right neighbor. We start it of by giving the last one (*right*) a value
of 0. Then we wait until they are finished and print it. 

# Compile and run
To compile the above program you

    8g chain.go 
    8l -o chain chain.8

And then run it

    ./chain

On my crappy laptop this takes about 2.3 seconds. Not too bad :)

 Vim
For Go code editing, I've added the following to my `~/.vimrc`

    autocmd Filetype go set textwidth=0
    autocmd Filetype go set noexpandtab
    autocmd Filetype go set tabstop=8
    autocmd Filetype go set shiftwidth=8
    autocmd Filetype go set softtabstop=8
    autocmd Filetype go set number
    autocmd Filetype go command! Fmt %!gofmt

The last line adds a new command which reformats your Go code. Just
do `<ESC>:Fmt` and you'll end up with properly formatted Go code.
The coding style rules for Go are simple. Its what you get when you
run it through to `gofmt`.


