Go – Golang

Go – also named Golang to prevent confusion – is a new and well designed programming language, developed by Google. This page has a write-up of information found in (official) documentation for easier second-access and possibly a faster entry to programming in Go.

the following is in creole-syntax – a conversion to HTML may follow

Fuck. Joomla/TinyMCE fucks this up somehow. FFS! – Upload Postponed. Read as far as you can, if you dare.

Go

Clone/Pull via hg (mercurial)

Compile

cd go/src
./all.bash

Prepare environment

export GOROOT=$HOME/go
export GOARCH=amd64
export GOOS=linux
export PATH=$PATH:$GOROOT/bin

Compilation Workflow

Source

/*
 * Small sample program
 */
package main

import fmt "fmt" // Package implementing formatted I/O.

func main() {
  fmt.Printf("Hello, world; or ???????? ?????; or ????? ??\n")
}

Startpoint: main package: main function
Strings are UTF-8
go fmt to format source (tab-indent)
Don’t need to put semicolons at all, but can (e.g. for multiple statements on one line).
Opening (if and the like) brace should be (sometimes have to be) on same line.

for i := 0; i &lt; flag.NArg(); i++ {<br />  if i &gt; 0 {<br />  }<br />}

Introduce declarations: var, const, type, import

const (
  Space = " "
  Newline = "\n"
)

can also be written as (/is shorter for)

const Space = " "
const Newline = "\n"

var s string = "" can be simplified to var s = "" or s := ""

for is the only loop construct in Go (no while or do).

main.main has no return type. To signal an erroneous return, call os.Exit(1)

os.Args for slice of cli arguments (eg used by flag package)

Strings are immutable values. Variable can be reassigned. The following is legal code:

s := "hello"
if s[1] != 'e' { os.Exit(1) }
s = "good bye"
var p *string = &s *p = "ciao"

However, as immutable, invalid is: s[0] = 'x' and (*p)[1] = 'y'.

Array

var arrayOfInt [10]int

Arrays are variables with values and size, slices are references without size.

arrayOfInt[1:3]

Value-assignment - compound value constructor: [3]int{1,2,3}

Array as fn-argument: you probably want to use a slice

[:] will slice the whole array (array to slice)

func sum(a []int) int { // returns an int …

Callable via

s := sum([3]int{1,2,3}[:])

or

s := sum([...]int{1,2,3}[:])

Maps

m := map[string]int{"one":1 , "two":2}

len() and range work on strings, arrays, slices, maps, channels

for ind, val := range a { 1, 3, 4 }

More

type T struct { a, b int }
var t *T = new(T)
OR t := new(T)
m := make(map[string]int)
var m map[string]int // will be nil
pointers `*` and `&`, like C

func (file *File) Read(b []byte) (ret int, err os.Error) {

“Function of(/for /in) type File, accessible via /file/, named Read (exported) with argument b of type []byte (array of bytes) and returns an int and an os.Error”

Printf(format string, v …interface{}) (n int, errno os.Error)

var u64 uint64 = 1<<64-1
fmt.Printf("%d %d\n", u64, int64(u64))

%v for automatic print in appropriate style

fmt.Printf("%v %v %v\n", u64, t, a) -> 18446744073709551615 {77 Sunset Strip} [1 2 3 4]

fmt.Print(u64, " “, t, " “, a, “\n”)

fmt.Println(u64, t, a)

String() method

%T: string representation of the type of a value

type assertion: given v and interface Stringer

s, ok := v.(Stringer)

Packages

  • fmt: formatted I/O
  • os:
  • flag: command line option parser