From Ruby to Go

Getting to know Go, a fun little language from Google.

I’m not planning on leaving the lovely Ruby ecosystem anytime soon, but it is always nice to peek over the fence to other programming languages once in a while.

I’ve now decided to take a closer look at Go.

How is it different from Ruby?

Go is VERY different from Ruby, but these are some of the highlights:

  • Go is a statically typed, compiled language.
  • Go has Pascal/Modula-style syntax: name before type.
  • Go does not have classes or subtype inheritance.
  • Go does not support default function arguments.
  • Go has no try-catch control structures for exceptions.
  • Enforced (automatic) code formatting via the gofmt tool.
  • Functions in Go can return multiple values.
  • Export syntax (upper case initial letter for public method names)
  • Concurrency via CSP

Background

Go is a compiled, garbage-collected, concurrent programming language developed by Google. The initial design of Go was started in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.

Getting started

You don’t even have to install Go to start experimenting with it.

The web site has a neat interactive Tour of Go and there is also the Go Playground, a web service that receives a Go program, compiles, links, and runs the program inside a sandbox, then returns the output. (A little bit like Try Ruby)

Installing Go

Go is available from Homebrew

$ brew install go

You can also install Go by downloading it from the official downloads page.

Vim configuration

A vim syntax file is included in the Go distribution under misc/vim/

To use all the Vim plugins, add these lines to your vimrc:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

I installed Go using Homebrew so my $GOROOT is /usr/local/Cellar/go/1.0.3

Examples

Hello World

Let’s write the canonical getting started program, Hello world!

hello_world.go

package main

import "fmt"

func main() {
    fmt.Println("hej världen!")
}

You use go run to run the program:

$ go run hello_world.go
hej världen!

You can also use go get to compile the program into a stand alone excecutable:

$ go get
$ ./hello_world
hej världen!

(This requires your code to be located in its own directory)

A simple Web application

Time for something a bit more interesting. The language was originally designed with networking in mind, that means it is fairly straight forward to write web applications in Go.

I was looking around for something similar to Sinatra and found pat by Blake Mizerany (The original author of Sinatra)

Pat is a Sinatra style pattern muxer for Go’s net/http library.

hello_pat.go

package main

import (
  "io"
  "net/http"
  "github.com/bmizerany/pat"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
  io.WriteString(w, "hej, "+req.URL.Query().Get(":name")+"!\n")
}

func main() {
  m := pat.New()
  m.Get("/hello/:name", http.HandlerFunc(HelloServer))

  http.Handle("/", m)
  http.ListenAndServe(":12345", nil)
}

Deploying Go on Heroku

You can deploy Go applications on Heroku thanks to the Go Buildpack, but I’m not going to go into details about this since Mark McGranaghan wrote a very good blog post on this very subject.

Learn more about Go