## Go

I've been meaning to learn Go for a while now. I've recently come to renew an interest in simple, no non-sense languages like Python, and what I imagine Go to be. The resources I'll be using are the go specification, go tour, and effective go. A lot of this is straight from these sources, with my commentary of how I come to understand it.

# #Packages

Packages can be imported with the import statement. They can also be grouped inside parentheses. The containing package can be specified with the package statement. Names that begin with capitalized letters are exported by the package:

# #Functions

When there are consecutive named function parameters of the same type, we can omit the type of all but the last:

Functions can return multiple values:

Result values can be named and set within the function, in which case return returns them as they are:

Functions are also values, and can be used to create closures:

# #Variables

Variables are declared with var. Multiple variables can be initialized in a tuple-assignment fashion, in which case the type can be omitted as it will be inferred from the initializers. Inside functions, := can be used in place of a var declaration with inferred type. Variables can be declared constant with const but not using the := syntax:

Numeric constants have arbitrary precision and don't overflow.

The following are the available types in Go. Type conversions are possible through T(v).

# #Control Structures

There's only one looping construct and that's the for loop. The for loop in Go doesn't use parentheses around the parameters, but the braces around the code to loop are mandatory.

As in C, pre and/or post conditions can be omitted. If both are omitted, it's just like a while loop:

Further still, the condition can be omitted to achieve a forever-loop.

if conditions can take a statement to run before the condition, scoped until the end of the if block (i.e. including else blocks):

The switch statement case bodies break by default, unless they end in a fallthrough statement:

The condition on the switch statement can be omitted, in which case it acts like a long if-then-else chain.

# #Structures

Structures can be defined with the struct keyword and given a name with type. Fields are accessed using a dot as in other languages. When creating a struct, any non-initialized field takes on that type's default value:

# #Pointers

There are pointers but no pointer arithmetic. Indirection through pointers is transparent:

A value of type T can be allocated with the new function:

# #Arrays and Slices

An array of n type T elements is created with the [n]T syntax. A slice points to an array and includes a length and capacity. Slices can be re-sliced with the syntax s[lo:hi] as in Python slices, which essentially points to a region of the same array. Slices can be created with make which takes the type of array, the length and optional capacity. The range function can be used to iterate over a slice or map, yielding an index, value per iteration, or if the value isn't needed simply omit it:

# #Maps

Maps take the form map[key]value. A nil map is empty and can't be assigned to. Map literals accept a trailing comma. When reading a map element, if the key doesn't exist in the map, the value returned is the default value for the map value type:

# #Methods

While Go doesn't have classes, it has a syntax for defining methods on structures. In the function declaration, the method receiver appears before the function name. This can also be done on any type we create, such as with type, but not on types from other packages or basic types. A pointer receiver can be modified and doesn't have to copy the entire structure on the method call, just like in C/C++:

# #Interfaces

Interfaces types are defined by a set of methods. A value of interface type can hold any value that implements those methods. It's important to note that types implement interfaces implicitly, there's no explicit declaration of intent as with Haskell typeclasses (class T where) or traditional Java interfaces (class A implements B):

Interfaces can also be composed of other interfaces:

# #Errors

In Go, an error is anything that can be described as a string. Interface type error defines a single method Error that returns a string:

# #Concurrency

A goroutine is a lightweight thread managed by the Go runtime. The evaluation of the function and its arguments occurs in the current goroutine but execution of the function occurs in a new goroutine:

Channels are typed conduits through which data of that type can be sent, a lot like Haskell channels. The <- operator is used to send and receive values, where the data travels in the direction of the arrow. Sending and receiving blocks until the other side is ready, allowing simple synchronization of the different goroutines:

There are also bounded, buffered channels, in which case the buffer doesn't block on send as long as the buffer isn't full, nor on receive as long as the buffer isn't empty:

Channels can be closed with the close function. Receivers can test if the channel is closed by accepting the second return value. Alternatively, looping with a range on the channel will read values until the channel is closed:

The select statement lets a goroutine wait on multiple communication operations, a lot like the POSIX select() system call. It'll block until one of its cases can run, randomly choosing if multiple are ready. A default case can be specified for when no other case is ready, particularly useful when the select is used within a forever loop, as is usually the case. The default case is usually used to try a send or receive without blocking:

December 20, 2013