Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Parallel Computing in Go

Fabian Becker
November 17, 2012

Parallel Computing in Go

Fabian Becker

November 17, 2012

Transcript

17 slides
  1. What does it offer? • lightweight syntax • statically typed

    • garbage-collected (yay!) • concurrent • compiles to machine code
  2. Anything else? • free and open source • enforced code

    style • duck typing (type inference) • static typing • fast
  3. Example import ( “fmt” ”http” ) func handler(c *http.Conn, r

    *http.Request) { fmt.Fprintf(c, “Hello, %s\n”, r.URL.Path[1:]) } func main() { http.ListenAndServe(“:8080”, http.HandlerFunc(handler)) }
  4. Concurrency • Communicating Sequential Processes – Hoare, 1978 • Goroutines

    – Lighter than Threads – 100.000s – Channels • “Don't communicate by sharing memory; share memory by communicating.”
  5. Example #1 var a string func f() { a =

    “Hello World” } func main() { go f() println(a) }
  6. Example #2 var c = make(chan int, 1) var a

    string func f() { a = "hello, world" c <­ 0 } func main() { go f() <­c print(a) }
  7. Goroutines • Run on a set of threads • Ordinary

    functions (coroutines) – “go” keyword • Blocking – System calls block – Handled by the scheduler – Non-blocked routines moved to another thread • Memory efficient!
  8. Channels • Typed • “<-” operator channel := make(chan string)

    • Worker function declaration: func worker(input string, results chan<­ string) { // do work results <­ result }
  9. Channels #2 // A typed queue c := make(chan int)

    // Buffered vs. unbuffered unbuf := make(chan int) buf := make(chan int, 100) // Send to a channel c <­ 10 // Receive from a channel i := <­c
  10. Testing • Go comes with a lightweight test suite –

    “go test” • Automated tests are great! func TestFoo(t *testing.T) { ... }
  11. Benchmarking • Benchmark all the things! func BenchmarkHello(b *testing.B) {

    for i := 0; i < b.N; i++ { fmt.Sprintf("hello") } } • Nice for optimizing critical sections of your code
  12. Who uses Go? • Google (No shit Sherlock!) • CloudFlare

    • Heroku • SoundCloud • Novartis • ...

Other Decks in Technology