Go Programming Language

Information in this document is taken (often directly) from golang.org's tour.

Building and Running

Information in this section is from How to Write Go Code on the golang website.

The go tool is the standard way to fetch, build, and install Go packages and commands. Go programmers typically keep all their Go code in a single workspace. A workspace contains many version control repositories. Each repository contains one or more packages. Each package consists of one or more Go source files in a single directory.

A workspace is a directory hierarchy with three directories at its root: src, pkg, and bin. The go tool builds source packages and installs the resulting binaries to the pkg and bin directories. The src subdirectory typically contains multiple version control repositories.

The GOPATH environment variable specifies the location of your workspace.

An import path is a string that uniquely identifies a package. A package's import path corresponds to its location inside a workspace or in a remote repository. If you have a GitHub account at github.com/user, that should be your base path.

Now you can build and install a program with the go tool:

go install github.com/elliotpenson/hello

You can also omit the package path if you run go install from the package directory.


Programs start running in package main.

package main

By convention, the package name is the same as the last element of the import path. For instance, the "math/rand" package comprises files that begin with the statement package rand.

Imports can be grouped into a parenthesized, "factored" import statement.

import (

In Go, a name is exported if it begins with a capital letter. When importing a package, you can refer only to its exported names.


A function can take zero or more arguments.

func add(x int, y int) int {
        return x + y

Notice that the type comes after the variable name.

When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

func add(x, y int) int {
        return x + y

Multiple Results

A function can return any number of results.

func swap(x, y string) (string, string) {
        return y, x

Named Return Values

Go's return values may be named. If so, they are treated as variables defined at the top of the function. These names should be used to document the meaning of the return values.

A return statement without arguments returns the named return values. This is known as a naked return. Naked return statements should be used only in short functions. They can can harm readability in longer functions.


A var statement declares a list of variables; as in function argument lists the type is last. A var statement can be at package or function level.

package main

import "fmt"

var c, python, java bool

func main() {
        var i int
        fmt.Println(i, c, python, java)


A var declaration can include initializers, one per variable. If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

var i, j = 1, 2

Short Variable Declarations

Inside a function, the `:=` short assignment statement can be used in place of a var declaration with implicit type. Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

k := 3


Go's basic types are:



int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

Zero Values

Variables declared without an explicit initial value are given their zero value. The zero value is 0 for numeric types, false for the boolean type, and "" (the empty string) for strings.

Type Conversions

The expression T(v) converts the value v to the type T.

i := 42
f := float64(i)
u := uint(f)


Constants are declared like variables, but with the const keyword. Constants can be character, string, boolean, or numeric values. Constants cannot be declared using the := syntax.

const Pi = 3.14


Go has only one looping construct, the for loop. The basic for loop looks similar to C (with three components) but without parenthesis.

for i := 0; i < 10; i++ {
        // something

The init and post statement are optional. At that point you can drop the semicolons: C's while is spelled for in Go.

sum := 1
for sum < 1000 {
        sum += sum

If you omit the loop condition it loops forever.


No need for parens in Go's if statements, but the braces are required.

if x < 0 {
        // body

Like for, the if statement can start with a short statement to execute before the condition. Variables declared by the statement are only in scope until the end of the if.

if v := math.Pow(x, n); v < lim {
        return v

if and else

Variables declared inside an if short statement are also available inside any of the else blocks.


A switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression.

Go's switch is like the one in C, except that Go only runs the selected case, not all the cases that follow.

package main

import (

func main() {
        fmt.Print("Go runs on ")
        switch os := runtime.GOOS; os {
        case "darwin":
                fmt.Println("OS X.")
        case "linux":
                // freebsd, openbsd,
                // plan9, windows...
                fmt.Printf("%s.", os)

Switch cases evaluate from top to bottom, stopping when a case succeeds.

Switch without a condition is the same as switch true. This construct can be a clean way to write long if-then-else chains.


A defer statement defers the execution of a function until the surrounding function returns. The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

func main() {
        defer fmt.Println("world")


Deferred function calls are pushed onto a stack. When a function returns its deferred calls are executed in last-in-first-out order.


Go has pointers. A pointer holds the memory address of a value. The type *T is a pointer to a T value. Its zero value is nil.

var p *int

The & operator generates a pointer to its operand.

i := 42
p = &i

The * operator denotes the pointer's underlying value.

fmt.Println(*p) // read i throught the pointer p
*p = 21         // set i through the pointer p

This is known as dereferencing or indirecting.

Unlike C, Go has no pointer arithmetic.


A struct is a collection of fields.

package main

import "fmt"

type Vertex struct {
        X int
        Y int

func main() {
        fmt.Println(Vertex{1, 2})

Struct fields are accessed using a dot.

v := Vertex{1, 2}
v.X = 4

Struct Pointer

Struct fields can be accessed through a struct pointer. To access the field X of a struct when we have the struct pointer p we could write (*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.

v := Vertex{1, 2}
p := &v
p.X = 1e9

Struct Literals

A struct literal denotes a newly allocated struct value by listing the values of its fields.

var v1 = Vertex{1, 2}  // has type Vertex

You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)

var v2 = Vertex{X: 1}  // Y:0 is implicit

The special prefix & returns a pointer to a struct value.

p = &Vertex{1, 2}  // has type *Vertex


The type [n]T is an array of n values of type T. The expression

var a [10]int
a[0] = 1

primes := [6]int{2, 3, 5, 7, 11, 13}

declares a variable a as an array of ten integers. An array's length is part of its type, so arrays cannot be resized. This seems limiting, but don't worry; Go provides a convenient way of working with arrays.


An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays.

The type []T is a slice with elements of type T.

A slice is formed by specifying two indices, a low and high bound, separated by a colon:

a[low : high]

This selects a half-open range which includes the first element, but excludes the last one.

The following expression creates a slice which includes elements 1 through 3 of a:


Slices are like references to arrays. A slices does not store any data, it just describes a section of an underlying array. Changing the elements of a slice modifies the corresponding elements of its underlying array. Other slices that share the same underlying array will see those changes.

Slice Literals

A slice literal is like an array literal without the length

This is an array literal

[3]bool{true, true, false}

And this creates the same array as above, then builds a slice that references it:

[]bool{true, true, false}   

Slice Defaults

When slicing, you may omit the high or low bounds to use their defaults instead. The default is zero for the low bound and the length of the slice for the high bound.

For the array

var a [10]int

these slice expressions are equivalent:


Slice Length and Capacity

A slice has both a length and a capacity. The length of a slice is the number of elements it contains. The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.

The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s).

You can extend a slice's length by re-slicing it, provided it has sufficient capacity.

The zero value of a slice is nil. A nil slice has a length and capacity of 0 and has no underlying array.

var s []int
if s == nil {

Creating a Slice With Make

Slices can be created with the built-in make function; this is how you create dynamically-sized arrays.

The make function allocates a zeroed array and returns a slice that refers to that array:

a := make([]int, 5)  // len(a)=5

To specify a capacity, pass a third argument to make.

b := make([]int, 0, 5)  // len(b)=0, cap(b)=5

Slices of Slices

Slices can contain any type, including other slices.

board := [][]string{
        []string{"_", "_", "_"},
        []string{"_", "_", "_"},
        []string{"_", "_", "_"},
board[0][0] = "X"

Slice Appending

It is common to append new elements to a slice, and so Go provides a build-in append function. The first parameter s of append is a slice of type T, and the rest are T values to append to the slice.

var s []int
s = append(s, 0)
s = append(s, 2, 3, 4)

If the backing array of s is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.


The range form of the for loop iterates over a slice or map. When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element of that index.

pow := []int{1, 2, 4, 8, 16, 32, 64, 128}
for i, v := range pow {
        fmt.Printf("2**%d = %d\n", i, v)

You can skip the index or value by assigning to _. If you only want the index, drop the ", value" entirely.


A map maps keys to values. The zero value of a map is nil. A nil map has no keys, nor can keys be added. The make function returns a map of the given type, initialized and ready for use.

var m map[string]Vertex
// ...
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
        40.68433, -74.39967
fmt.Println(m["Bell Labs"])

Map Literals

Map literals are like struct literals, but the keys are required.

var m = map[string]Vertex{
        "Bell Labs": Vertex{
                40.68433, -74.39967,
        "Google": Vertex{
                37.42202, -122.08408,

If the top-level type is just a type name, you can omit it from the elements of the literal.

var m = map[string]Vertex{
        "Bell Labs": {40.68433, -74.39967},
        "Google":    {37.42202, -122.08408},

Mutating Maps

Insert or update an element in map m:

m[key] = elem

Retrieve an element:

elem = m[key]

Delete an element:

delete(m, key)

Test that a key is present with a two-value assignment:

elem, ok := m[key]

If key is in m, ok is true. If not, ok is false. If key is not in the map, then elem is the zero value for the map's element type.

Function Values

Functions are values too. They can be passed around just like other values (as arguments and return values).

package main

import (

func compute(fn func(float64, float64) float64) float64 {
        return fn(3, 4)

func main() {
        hypot := func(x, y float64) float64 {
                return math.Sqrt(x*x + y*y)

Function Closures

Go functions may be closures. A closure if a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is bound to the variables.


Go does not have classes. However, you can define methods on types. A method is a function with a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name.

In this example, the Abs method has a receiver of type Vertex named v.

type Vertex struct {
        X, Y float64

func (v Vertex) Abs() float64 {
        return math.Sqrt(v.X*v.X + v.Y*v.Y)

func main() {
        v := Vertex{3, 4}

A method is just a function with a receiver argument.

You can declare a method on non-struct types, too. In this example we see a numeric type MyFloat with an Abs method.

type MyFloat float64

func (f MyFloat) Abs() float64 {
        if f < 0 {
                return float64(-f)
        return float64(f)

func main() {
        f := MyFloat(-math.Sqrt2)

You can only declare a method with a receiver whose type is defined in the same package as the method. You cannot declare a method with a receiver whose type is defined in another package (which includes the built-in types such as int).

Pointer Receivers

Methods with pointer receivers can modify the value to which the receiver points (as Scale does here). Since methods often need to modify their receiver, pointer receivers are more common than value receivers.

func (v *Vertex) Scale(f float64) {
        v.X = v.X * f
        v.Y = v.Y * f

With a value receiver, the Scale method operates on a copy of the original Vertex value. (This is the same behavior as for any other function argument.)

Unlike functions with pointer arguments, methods with pointer receivers can take either a value or a pointer as the receiver. Likewise, methods with value receivers take either a value or a pointer as the receiver when they are called.

There are two reasons to use a pointer receiver. The first is so that the method can modify the value that its receiver points to. The second is to avoid copying the value on each method call. This can be more efficient if the receiver is a large struct, for example.

In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both.


An interface type is defined as a set of method signatures. A value of interface type can hold any value that implements those methods.

A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. Implicit interfaces decouple the definition of an interface from its implementation, which could then appear in any package without prearrangement.