GoLang

A lot of teams are at least investigating GoLang. I love Node but started learning GoLang because the last place I worked had some apps written with it. I also started learning out of curiosity. I created a github repo with some examples.

Learning GoLang

You usually have to start with a hello world. But the best way to learn programming is with TDD.

Setup

First install go and then setup your Go path.

Lets test the setup by creating a folder called $GOPATH/src/learning. Paste the below into a file called hello.go:

package main

import "fmt"

func main() { fmt.Printf("hello, world\n") }

To execute use go run hello.go.

Go is also compiled into an executable. To do so run:

cd learning
go build hello.go // this creates the hello executable
./hello

The package main tells GoLang that this is an executable file and not a library. An executable file must also have a func main(). If it is named something other than main that would be a library.

The fmt is a package that handles input/output, in this case printing to stdout.

Offline Go

If you have go installed, run:

godoc -http=:6060

Then open to http://127.0.0.1:6060/ in your browser.

Packages/Libraries

A package, or library, groups together code which is utilised by another program.

Types

Strongly typed language.

bool, string, int, float64

Go word size is the memory address size. Based on your machine, 32bit, 64bit etc. An integer is based on your word size, so on a 64bit machine it will be 64 bits long, 32bits on a 32 bit machine.

Go zero values:

  • nil
  • 0
  • false
  • “”

Testing and TDD

Testing, like a lot of languages, is included in GoLang.

To run a test:

go test --cover

Variables

Variable names. I can't beleive I have to mention this. Reading a lot of peoples interpretations of pikestyle reinforces my opinion of the human race. Oh yeah, you commute on the London tube every day for years and lets hear your opnion. Anyway, I digress. Pike clearly mentions 'clarity of expression'. The variable should describe exactly what it is. Our conventions for i in for loops is totally fine. Anyone who has programmed will know this one. That doesn't mean to say that all variables should be single letters does it? Code is written for humans. Beautiful code should read like well written prose.

You can do multiple assignment:

a, b, c := 10, 100, 1000
fmt.Println(a, b, c)

Arrays and Slices

So Go does a lot of things for you. For this example we want to demonstrate arrays and slices. So lets have seperate files for this. Oh, so how do you import these files then? Aahhh, you don't have to do anything. Well, apart from change the run command to:

go run main.go array.go slice.go
go build main.go array.go slice.go

The run command needs to group all these files into an executable.

Rarely use arrays directly as they are pretty inflexible. Use slices instead. Of course arrays have their uses.

You will see the reserved word 'make'. This function takes care of allocating memory for the slice in this example.

make does a few things, one is creating a channel, takes care of allocating memory for a slice.

Functions

func [name]([[paramName/s,] type]) [[[name]return type], {(return types,)] {
...
}

Functions beginning with a capital letter are exported. 'Fat', 'Println' etc.

Structs

A struct is a custom type, which is an aggregate of other types.

In structs, always have attributes listed from shortest length of the data type. int8, int16 etc as go pads out the structure with memory allocation based on the first longest?

Pointers

Go is pass by value. To pass by reference you need to use pointers in the calling function parameter (*) and use 'address of' in the callee function parameters and body (&) &[variable name] i.e. &count.

pointers/pointer.go

Go routines

All Go you write gets run on a goroutine - an OS thread.

Channels

Channels are pipes that connect concurrent goroutines.

HTTP Server

http-server/server.go

Pipes

Time and Tickers

http-json/http-json.go

Command Line

Race Conditions

References