go  

Sep 19, 2014 • Michael Chen

Go is an open source programming language. Go source can be easily compiled into native binaries of several platforms, which brings the potential of application programming. I’m learning the new language on my spare time. Here is my notes for Go language. The contents may change during my learning process.

Install Go on Unix

The main toolchain of Go language can be placed in any place and you need to set GOROOT and PATH variable to use these tools. Besides, you need a separated place to hold third-party libraries and set GOPATH and PATH variable to access it. A possible way is installing the main toolchain to /usr/local and place third-parties libraries under the subdirectory of home directory.

export GOROOT=/usr/local/go
export GOPATH=$HOME/lib/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Learning Resources

Websites

Books

  • Programming in Go, Mark Summerfield, Addison-Wesley Professional, 2012
  • The Go Programming Language Phrasebook, David Chisnall, Addison-Wesley Professional, 2012
  • An Introduction to Programming in Go, Caleb Doxsey, CreateSpace Independent Publishing Platform, 2012. There are also free online edition of this book.
  • The Way To Go, Ivo Balbaert, iUniverse, 2012

Get help

Use godoc command to get help message for Go.

Look up a package. Because the message is long, using less is recommanded.

$ godoc fmt | less

Look up a function

$ godoc math Sin

Query by keyword

$ godoc -q sin

Start a local server.

$ godoc -http=":6060"

Create a Go Project

Before creating your own Go project, read “How to Write Go Code”. Go code must be kept inside a workspace. Here is my demo toy project.

+-- Makefile
+-- main.go
+-- src/
    +-- stats/
        +-- basic.go

The content of main.go is as follows:

package main

import (
	"fmt"
	"os"
	"strconv"
	"stats"
)

func main() {
    size := len(os.Args)
    if size <= 1 {
        os.Exit(0)
    }
    numbers := make([]float64, size)
    for i, x := range os.Args {
        if i == 0 {
            continue
        }

        num, _ := strconv.ParseFloat(x, 64)
        numbers[i] = num
    }
    sum := stats.Sum(numbers)
    fmt.Println(sum)
}

The above code converts all command arguments to float numbers; calculates the sum; prints out the result.

The content of basic.go is as follows:

package stats

func Sum (numbers []float64) float64 {
    var total float64;
    for _, x := range numbers {
        total += x
    }
    return total
}

The above code calculates the sum of an array of float numbers and return it.

The trick to compile Go project outside main Go directory is setting GOPATH in the command line interface. I writed the step in Makefile.

PROGRAM=sum

$(PROGRAM):
	GOPATH=`pwd` go build -o $(PROGRAM) main.go
clean:
	rm -f $(PROGRAM)

Install third-party libraries

To install third-party libraries, use go get command. go get receives importpath in command-line and install related libraries.

To search third-party libraries, try gowalker.org or go-search.org. I’m still trying the best search site for Go.