DEV Community

Cover image for Goroutines demystified
bluepaperbirds
bluepaperbirds

Posted on

Goroutines demystified

In programming languages, code is often split up into functions. Functions help to make code reusable, extensible etc.

In Go there's a special case: goroutines. So is a goroutine a function? Not exactly. A goroutine is a lightweight thread managed by the Go.

If you call a function f like this:

f(x)

it's a normal function. But if you call it like this:

go f(x)

it's a goroutine. This is then started concurrently.

If you are new to Go, you can use the Go playground

Goroutine examples

Try this simple program below:

package main

import (
    "fmt"
    "time"
)

func say(s string) {
    for i := 0; i < 3; i++ {
        time.Sleep(100 * time.Millisecond)
        fmt.Println(s)
    }
}

func main() {
    go say("thread")
    say("hello")
}

Execute it with:

go run example.go

So while say('hello') is synchronously executed, go say('hello') is asynchronous.

Consider this program:

package main

import (
    "fmt"
    "time"
)

func main() {
    go fmt.Println("Hi from goroutine")
    fmt.Println("function Hello")

    time.Sleep(time.Second) // goroutine needs time to finish
}

Then when I ran it:

function Hello
Hi from goroutine

Program exited.

As expected, the goroutine (thread) started later.

Related links:

Top comments (0)