{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Go for Science: Programming beyond Python\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Gophers\n" ] } ], "source": [ "import \"fmt\"\n", "n, err := fmt.Println(\"Hello, Gophers\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Features\n", "\n", "- Statically typed\n", "- Compiled\n", "- Quick and easy cross-compilation (Windows, Darwin, Linux, x86, ARM)\n", "- Garbage collection\n", "- Memory safe\n", "- Built-in concurrency\n", "- Fast compiler, fast runtime\n", "- Interoperates with C code (cgo)\n", "\n", "> \"Like C, but sane\"\n", " Achilleas Koutsou, 2018" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why we think it's *cool*\n", "\n", "- Open source!\n", "- Fast growing (both community and ecosystem)\n", "- *Clean* and *small*:\n", " - Handful of keywords\n", " - Automatic formatting\n", "- Designed for concurrency\n", "- Easy testing (for robust code)\n", "- Statically compiled binaries\n", " - Makes it **very** easy to distribute executables to various platforms\n", "\n", "..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why we think it's cool, part deux\n", "\n", "- Automatic documentation generation and distribution:\n", " - Function and package doc strings get automatically hosted on godoc.org\n", "- Fast evolving but respects backwards compatibility\n", "- The compiler is seriously super fast\n", "- Easy and fun to learn:\n", " - Go Tour quickly teaches everything you need to know to start working: https://tour.golang.org\n", " - Online *playground* makes it easy to test snippets and share complete examples: https://play.golang.org\n", " \n", "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)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "![Brace Style](./images/brace-style.jpg)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Other people like it too\n", "![Most Loved](./images/most-loved-wanted-2017.png)\n", "\n", "Source: Stack Overflow developer survey, 2017 https://insights.stackoverflow.com/survey/2017#technology" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why *you* might think it's cool\n", "\n", "- Fast and easy to learn (we went over this already)\n", "- Good (fast) for number crunching while still feeling like scripting\n", "- Concurrency for cluster analysis or multithreaded simulations\n", "- Easy to write, statically link, and crosscompile for all platforms (e.g., experimental setups using Raspberry Pi)\n", "- Scientific computing libraries with active dev communities" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example: Basic features" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "// C-like structs\n", "type NewStruct struct {\n", " Prompt string\n", " Number int\n", "}\n", "\n", "// Can define methods on structs\n", "func (ns NewStruct) ToString() string {\n", " // C-like print directives and formatting\n", " return fmt.Sprintf(\"%s: %d\", ns.Prompt, ns.Number)\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number: 420\n" ] } ], "source": [ "import \"log\"\n", "\n", "// Type inference with :=\n", "number := 10 * 42\n", "news := NewStruct{\"Number\", number}\n", "s := news.ToString()\n", "// Explicit and localised error handling\n", "n, err := fmt.Println(s)\n", "if err != nil {\n", " log.Fatal(\"Oh no! Print is broken\")\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example: Concurrent simulations and analysis" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Concurrency vs. parallelism\n", "\n", "- Concurrency is about dealing with lots of things at once.\n", "- Parallelism is about doing lots of things at once.\n", "- Not the same, but related.\n", "\n", "- Concurrency is about structure, parallelism is about execution.\n", "\n", "- Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.\n", "\n", "Taken from https://talks.golang.org/2012/waza.slide#8\n", "\"Concurrency is not Parallelism\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "![Concurrency sketch](./images/concurrent-gophers.jpg)\n", "\n", "Taken from https://talks.golang.org/2012/waza.slide#19" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "ename": "ERROR", "evalue": "reflect.Value.Convert: value of type func(reflect.Value) cannot be converted to type func(chan int)", "output_type": "error", "traceback": [ "reflect.Value.Convert: value of type func(reflect.Value) cannot be converted to type func(chan int)" ] } ], "source": [ "import (\n", " \"fmt\"\n", " \"math/rand\"\n", ")\n", "\n", "func RunSim(name string, steps int) <-chan int {\n", "\n", " spikechan := make(chan int)\n", " go func() {\n", " defer close(spikechan)\n", " for t := 0; t < steps; t++ {\n", " if rand.Float32() > 0.8 {\n", " spikechan <- t\n", " }\n", " }\n", " return\n", " }()\n", "\n", " return spikechan\n", "}\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "ename": "ERROR", "evalue": "repl.go:3:10: undefined identifier: RunSim", "output_type": "error", "traceback": [ "repl.go:3:10: undefined identifier: RunSim" ] } ], "source": [ "func main() {\n", "\n", " a := RunSim(\"A\", 100)\n", " b := RunSim(\"B\", 100)\n", " c := RunSim(\"C\", 100)\n", "\n", " for {\n", " select {\n", " case ta, ok := <-a:\n", " if ok {\n", " fmt.Printf(\"A spiked at %d\\n\", ta)\n", " } else {\n", " a = nil\n", " }\n", " case tb, ok := <-b:\n", " if ok {\n", " fmt.Printf(\"B spiked at %d\\n\", tb)\n", " } else {\n", " b = nil\n", " }\n", " case tc, ok := <-c:\n", " if ok {\n", " fmt.Printf(\"C spiked at %d\\n\", tc)\n", " } else {\n", " c = nil\n", " }\n", " }\n", " if a == nil && b == nil && c == nil {\n", " break\n", " }\n", "\n", " }\n", "}\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Scientific libraries and projects\n", "\n", "- GoNum: Numerical computing in Go (Go's numpy)\n", "- GoNum/mat64: Linear algebra for float64 matrices\n", "- GoNum/plot: For plotting and visualisation\n", "- Machine learning libraries: go-cluster, go-deep, libsvm, neural-go\n", "- GoBot: IoT and specialised hardware (check https://gobot.io/documentation/platforms/ for a ridiculously long list of platforms and devices)\n", "- Jupyter kernel for Go notebooks (like this presentation)\n", "\n", "Find everything you need at https://github.com/avelino/awesome-go" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Questions & Comments welcome\n", "\n", "You can find this presentation at https://gin.g-node.org/G-Node/Go-CNS-talk-2018" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Go", "language": "go", "name": "gophernotes" }, "language_info": { "codemirror_mode": "", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "", "version": "go1.11" } }, "nbformat": 4, "nbformat_minor": 2 }