Go for Science: Programming beyond Python

In [1]:
import "fmt"
n, err := fmt.Println("Hello, Gophers")
Hello, Gophers

Features

  • Statically typed
  • Compiled
  • Quick and easy cross-compilation (Windows, Darwin, Linux, x86, ARM)
  • Garbage collection
  • Memory safe
  • Built-in concurrency
  • Fast compiler, fast runtime
  • Interoperates with C code (cgo)
"Like C, but sane"
                       Achilleas Koutsou, 2018

Why we think it's cool

  • Open source!
  • Fast growing (both community and ecosystem)
  • Clean and small:
    • Handful of keywords
    • Automatic formatting
  • Designed for concurrency
  • Easy testing (for robust code)
  • Statically compiled binaries
    • Makes it very easy to distribute executables to various platforms

...

Why we think it's cool, part deux

  • Automatic documentation generation and distribution:
    • Function and package doc strings get automatically hosted on godoc.org
  • Fast evolving but respects backwards compatibility
  • The compiler is seriously super fast
  • Easy and fun to learn:

Most G-Node projects started after 2016 are written in Go: GIN, GIN Client, GIN DOI, GIN Indexing Service, and GIN Validator (if it's GIN, it's probably Go).

Brace Style

Other people like it too

Most Loved

Source: Stack Overflow developer survey, 2017 https://insights.stackoverflow.com/survey/2017#technology

Why you might think it's cool

  • Fast and easy to learn (we went over this already)
  • Good (fast) for number crunching while still feeling like scripting
  • Concurrency for cluster analysis or multithreaded simulations
  • Easy to write, statically link, and crosscompile for all platforms (e.g., experimental setups using Raspberry Pi)
  • Scientific computing libraries with active dev communities

Example: Basic features

In [2]:
// C-like structs
type NewStruct struct {
    Prompt string
    Number int
}

// Can define methods on structs
func (ns NewStruct) ToString() string {
    // C-like print directives and formatting
    return fmt.Sprintf("%s: %d", ns.Prompt, ns.Number)
}
In [3]:
import "log"

// Type inference with :=
number := 10 * 42
news := NewStruct{"Number", number}
s := news.ToString()
// Explicit and localised error handling
n, err := fmt.Println(s)
if err != nil {
    log.Fatal("Oh no! Print is broken")
}
Number: 420

Example: Concurrent simulations and analysis

Concurrency vs. parallelism

  • Concurrency is about dealing with lots of things at once.
  • Parallelism is about doing lots of things at once.
  • Not the same, but related.

  • Concurrency is about structure, parallelism is about execution.

  • Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.

Taken from https://talks.golang.org/2012/waza.slide#8 "Concurrency is not Parallelism"

In [6]:
import (
    "fmt"
    "math/rand"
)

func RunSim(name string, steps int) <-chan int {

    spikechan := make(chan int)
    go func() {
        defer close(spikechan)
        for t := 0; t < steps; t++ {
            if rand.Float32() > 0.8 {
                spikechan <- t
            }
        }
        return
    }()

    return spikechan
}
reflect.Value.Convert: value of type func(reflect.Value) cannot be converted to type func(chan int)
In [5]:
func main() {

    a := RunSim("A", 100)
    b := RunSim("B", 100)
    c := RunSim("C", 100)

    for {
        select {
        case ta, ok := <-a:
            if ok {
                fmt.Printf("A spiked at %d\n", ta)
            } else {
                a = nil
            }
        case tb, ok := <-b:
            if ok {
                fmt.Printf("B spiked at %d\n", tb)
            } else {
                b = nil
            }
        case tc, ok := <-c:
            if ok {
                fmt.Printf("C spiked at %d\n", tc)
            } else {
                c = nil
            }
        }
        if a == nil && b == nil && c == nil {
            break
        }

    }
}
repl.go:3:10: undefined identifier: RunSim

Scientific libraries and projects

  • GoNum: Numerical computing in Go (Go's numpy)
  • GoNum/mat64: Linear algebra for float64 matrices
  • GoNum/plot: For plotting and visualisation
  • Machine learning libraries: go-cluster, go-deep, libsvm, neural-go
  • GoBot: IoT and specialised hardware (check https://gobot.io/documentation/platforms/ for a ridiculously long list of platforms and devices)
  • Jupyter kernel for Go notebooks (like this presentation)

Find everything you need at https://github.com/avelino/awesome-go

Questions & Comments welcome

You can find this presentation at https://gin.g-node.org/G-Node/Go-CNS-talk-2018