go
go { ... } runs a block on a new goroutine and returns at once.
Details
gohas typevoid. There is no handle to join or to read a result from: a goroutine hands back its result through a channel or a shared container, and signals completion through aWaitGroup.- The program exits when the script body ends, whatever other goroutines are still doing.
- A panic ends only the goroutine it occurs in, after that goroutine's deferred blocks run. The message and trace go to standard error, and the rest of the program continues.
- A
goblock is a boundary:returnin it ends the goroutine and takes no value,trydirectly in it is a compile error, andbreakandcontinuecannot reach a loop outside it. - Everything the starting goroutine did before the
gois visible to the new goroutine.
Basic examples
Start a goroutine and wait for it with a WaitGroup:
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
println("hello from a goroutine")
}
wg.wait()
println("done")hello from a goroutine
doneSend a goroutine's result back on a channel:
let results: Channel[int] = Channel.new()
go { results.send(6 * 7) }
println(results.receive())42Scope
A go block captures the variables around it. Each loop iteration binds a fresh loop variable, so every goroutine sees its own i. Each goroutine writes its own array slot:
let squares = Array.filled(4, 0)
let wg = WaitGroup.new()
for i in [0, 1, 2, 3] {
wg.add(1)
go {
defer { wg.done() }
squares[i] = i * i
}
}
wg.wait()
println(squares)[0, 1, 4, 9]A go block can call any function. Here the goroutine produces values that the script body reads:
func produce(out: Channel[int]) {
defer { out.close() }
for n in [1, 2, 3] {
out.send(n)
}
}
let numbers: Channel[int] = Channel.new()
go { produce(numbers) }
for n in numbers.values() {
println(n * 10)
}10
20
30A function can start a goroutine and return before it ends. return inside a go block ends the goroutine, not the function that started it:
func notify(user: string, out: Channel[string]) {
go {
if user.isEmpty() {
out.send("skipped")
return
}
out.send("sent to ${user}")
}
}
let out: Channel[string] = Channel.new()
notify("ann", out)
println(out.receive())
notify("", out)
println(out.receive())sent to ann
skippedA panic ends its goroutine and nothing else. Its deferred blocks still run, so defer { wg.done() } lets the wait return. The panic is reported on standard error, and the program goes on:
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
panic("worker failed")
}
wg.wait()
println("still running")still runningApplications
A worker reads jobs from a channel and calls each through Panic.catch, so a bug in one job does not end the worker:
func handle(job: string): int {
100 / job.length()
}
let jobs: Channel[string] = Channel.new()
let report: Channel[string] = Channel.new()
go {
defer { report.close() }
for job in jobs.values() {
let r = Panic.catch({ handle(job) })
let line = if r is Ok { "${job}: ${r.value}" } else { "${job}: ${r.error.message}" }
report.send(line)
}
}
go {
defer { jobs.close() }
for job in ["abcd", "", "ab"] {
jobs.send(job)
}
}
for line in report.values() {
println(line)
}abcd: 25
: division by zero
ab: 50Give up on a slow goroutine with a context deadline. The receive returns null when the context is done:
let reply: Channel[string] = Channel.new()
go {
Time.sleep(5s)
reply.send("late answer")
}
let ctx = Context.new(50ms)
println(reply.receive(ctx) ?? "timed out")timed outStop a goroutine with a context. The goroutine checks isDone between steps and passes the context into its sleep, so cancel ends the wait at once. The caller cancels and then waits for the goroutine to finish:
let ctx = Context.new()
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
while !ctx.isDone() {
Time.sleep(1s, ctx)
}
println("worker stopped: ${ctx.reason() ?? "running"}")
}
Time.sleep(20ms)
ctx.cancel()
wg.wait()
println("done")worker stopped: cancelled
doneCount across goroutines with an Atomic:
let hits = Atomic.new(0)
let wg = WaitGroup.new()
for _ in Array.range(0, 100) {
wg.add(1)
go {
defer { wg.done() }
hits.add(1)
}
}
wg.wait()
println(hits.load())100Properties and relations
A goroutine per element, each writing its own slot, joined with a WaitGroup, computes what Parallel.map computes. Parallel.map also bounds how many run at once:
func squareAll(xs: Array[int]): Array[int] {
let out = Array.filled(xs.length(), 0)
let wg = WaitGroup.new()
for e in xs.entries() {
wg.add(1)
go {
defer { wg.done() }
out[e.index] = e.value * e.value
}
}
wg.wait()
out
}
let xs = [3, 1, 4, 1, 5]
assert(squareAll(xs) == Parallel.map(xs, func(n: int, work: Context): int { n * n }, limit: 2))A write the starting goroutine makes before the go is visible inside the goroutine:
let settings = Map{ mode: "fast" }
settings["mode"] = "safe"
let done: Channel[bool] = Channel.new()
go {
assert(settings["mode"] == "safe")
done.send(true)
}
done.receive()Possible issues
The program exits when the script body ends. A goroutine that has not finished by then never does:
go {
Time.sleep(1s)
println("never printed")
}
println("script ends")script endsgo gives void. The value of its block is dropped:
let v = go { 42 }
println(v)voidA local assigned inside a go block never narrows outside it, since the goroutine may assign it at any moment. Send the value on a channel or write it into a container instead:
var hit: string | null = null
go { hit = "found" }
if hit != null { println(hit.length()) }
// error: string | null has no field or function length on every member: narrow the value firstA go block has no result, so try cannot return from it:
func load(): Result[int, Error] {
Result.ok(1)
}
func start(): Result[int, Error] {
go {
let n = try load() // error: try needs an enclosing function that returns a Result
println(n)
}
Result.ok(0)
}Handle the Result inside the block, or send it to the code that waits.
return in a go block takes no value:
go { return 1 } // error: 'return' takes no value in a go blockbreak and continue cannot leave a loop outside the go block:
for _ in [1, 2] {
go {
break // error: 'break' outside a loop
}
}A goroutine that panics before it signals completion leaves a waiter blocked for good. Put defer { wg.done() } first in the block, as the examples above do, so it runs on every exit.
Two goroutines that access the same variable, field, or element without synchronization, at least one of them writing, are a data race. A data race is a program error: a read sees some stored value but not a particular one, and the program may end with exit status 1. Guard shared state with an Atomic, a Mutex, or a channel, as in the counter above.
A goroutine that ignores its context runs until the program ends. Check isDone between steps and pass the context into waits, as the stopping example above does.
See also
defer · select · return · Channel · WaitGroup · Atomic · Mutex · Context · Parallel · Panic.catch · Concurrency
