An early try at programming-with-cartoons, in the spirit of _why’s Poignant Guide. Current work is at home.

Animals explaining goroutines and channels

A few animals marked up an introduction to goroutines and channels from https://go.dev/talks/2012/concurrency.slide#20 ! You can copy paste the below into a file, say simple.go, and run it with go run simple.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import ("fmt"
    "math/rand"
    "time")

func main() {
    c := make(chan string)
    go boring("boring!", c)
    for i := 0; i < 5; i++ {
        fmt.Printf("You say: %q\\n", <-c) // Receive expression is just a value.
    }
    fmt.Println("You're boring; I'm leaving.")
}

func boring(msg string, c chan string) {
    for i := 0; ; i++ {
        c <- fmt.Sprintf("%s %d", msg, i) // Expression to be sent can be any suitable value.
        time.Sleep(time.Duration(rand.Intn(1e3)) \* time.Millisecond)
    }
}

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 4.0 Attribution License.