Go, go, go

gopherI've decided to learn Go. Not the board game, but the programming language. Why? I've been wanting to learn a systems programming language for a while, but have so far avoided picking up my brick of a C++ manual.

If Go can really replace C++ when writing e.g. a high performancee server (I don't know enough to say that yet), then that is good news! For starters I'm doing

It's a 71 step tutorial. Each step is a code editor and some text. Sometimes the text is an exercise, which is the case for step 35 (implement Pic). This is the picture I created there by the way (Go program listed at EOM):

go

Go is a fun language to learn. One thing I stumble upon here in the beginning is writing basic statements like loops and variable assignments in a new way. This is because the differences compared to Java, C and Python (the main language I know) are subtle. I find it a little annoying, but that is only because I suddenly can't remember how to write a for loop :-) However, new syntax should not obscure the killer features (in my book) of this newish language, namely that it is easy to write like Python, and has performance like C++. Well, it's in the ball park of those languages. Seems like the perfect combination!

There are many syntax things that I like. These are the things that make Go "easy to write like Python". One common thing in programming is to retrieve values from a map, when the key might be missing. This is how to do that in Go, which does away with huge constructions around map lookups (if (m.haskey()), try/excepts, ...):

An example of new syntax. Elegant key-lookups:

# Doing away with scaffolding around map key-loopups! Yay!
# Because the key is missing, ok is False
package main
 
import "fmt"
 
func main() {
    m := make(map[string]int)
 
    v, ok := m["Missing Key"]
    fmt.Println("The value:", v, "Present?", ok)
    # prints: The value: 0 Present? false
}

Solutions: "A Tour of Go" exercises

Spoiler alert!! I'm not claiming these to be particular elegant, so I'm just recorded them so that I may look at them in a future where I'm an expert Go programmer. In that future I can visit this page again and laugh at my feeble attempts.

page 35:

# My program to create and image as part of "A Tour of Go".
package main
 
import "code.google.com/p/go-tour/pic"
 
func Pic(dx, dy int) [][]uint8 {
    pic := make([][]uint8, dy)
    for y := 0; y < dy; y++ {
        pic[y] = make([]uint8, dx)
        for x := 0; x < dx; x++ {
           pic[y][x] = uint8(x^y+(x+y)/2) 
        }
    }
    return pic
}
 
func main() {
    pic.Show(Pic)
}

Page 40:

package main
 
import (
    "code.google.com/p/go-tour/wc"
    "strings"
)
 
func WordCount(s string) map[string]int {
    wc := make(map[string]int)
    for _, word := range strings.Fields(s) {
        wc[word] += 1
    }
    return wc
 
}
 
func main() {
    wc.Test(WordCount)
}

Page 43:

package main
 
import "fmt"
 
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
    prev := 1
    cur := 0
    return func() int {
        res := prev + cur
        prev = cur
        cur = res
        return res
    }
}
 
func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.