google's go programming language

16.11.2009

google released a new programming language named go, a compiled, garbage-collected, concurrent programming language. its initial design was started in late of 2007 by rob pike and ken thompson.

for now it's available on the linux and mac os platforms. go doesn't support features such as exception handling, type inheritance, generic programming, method overriding and assertions but according to rob pike's presentation generic programming support is on the to-do list.

installing go on mac os x

if you read this entry i assume you're a developer and you've xcode installed. you need mercurial to fetch the repository so if you don't have it already, you need to install it.

the Go compilation environment depends on three environment variables that you should set in your .bashrc or .bash_profile, plus one optional variable:
export GOROOT=$HOME/go
export GOOS=darwin
export GOARCH=386
export GOBIN=$HOME/bin (optional)
export PATH=$GOBIN:$PATH

so we're ready to fetch the repository.
hg clone -r release https://go.googlecode.com/hg/ $GOROOT

now we're ready to build go. go into src dir of go and type
./all.bash

you may receive the following error but it's ok. don't mind it.
--- FAIL: http.TestClient
	Get http://www.google.com/robots.txt: dial tcp www.google.com:http: lookup www.google.com. no answer from server
--- FAIL: http.TestRedirect
	Get http://codesearch.google.com/: dial tcp codesearch.google.com:http: lookup codesearch.google.com. no answer from server

and we're done. in the examples of go's documentation, they're compiled with 6g but if you're in i386 architecture, your commands are 8g and 8l.

some snippets in go

here are some small code examples in go. let's start with the legendary factorial function.
func factorial_inner(n int, counter int, product int) (res int) {
  if counter > n {
    res = product;
  } else {
    res = factorial_inner(n, counter+1, product*counter);
  }
  return;
}

func factorial(n int) (res int) {
  res = factorial_inner(n, 1, 1);
  return;
}

in go you can declare the return value as a variable in the function header. i named the return values as res. then you can assign values to that variable. another thing that needs to be noticed is we declare the variable type after the variable like counter int.

you can also return multiple variables like in python for example. here's the good old fibonacci numbers as an example for this;
func fibonacci(n int) (val int, counter int) {
  if n == 0 {
    val = 1;
    pos = 0;
  } else if n == 1 {
    val = 1;
    pos = 1;
  } else {
    a, _ := fibonacci(n-1);
    b, _ := fibonacci(n-2);
    val = a+b;
    pos = n;
  }
  return;
}

to test for presence without worrying about the actual value, you can use the blank identifier, a simple underscore (_). the blank identifier can be assigned or declared with any value of any type, with the value discarded harmlessly.

anyway, i'll write something more about go later but this is just the first taste of it.

the go programming language
rob pike's tech talk
blog comments powered by Disqus