Back to blog

Code Snippet - Goroutines

A lovely language feature of Go for running functions concurrently.

package main

import(
    "fmt"
    "time"
)

const Interval int64 = 20;
const Max int = 10;

func f(threadName string) {
    for i := 0; i < Max; i++ {
        fmt.Println(i, "from", threadName)
        time.Sleep(time.Duration(Interval))
    }
}

func main() {
    go f("one")
    go f("two")
    time.Sleep(time.Duration(Interval) * time.Duration(Max))
}