Goroutines in Ada A goroutine is a lightweight thread of execution.
```ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
procedure Goroutines is
task type Goroutine is
entry Start (From : String);
end Goroutine;
task body Goroutine is
begin
accept Start (From : String) do
for I in 0 .. 2 loop
Put_Line (From & " : " & Integer'Image (I));
end loop;
end Start;
end Goroutine;
function Sleep (Seconds : Duration) return Boolean is
begin
delay Seconds;
return True;
end Sleep;
G1 : Goroutine;
G2 : Goroutine;
G3 : Goroutine;
begin
-- Suppose we have a function call f(s). Here’s how we’d call that in the usual way,
-- running it synchronously.
G1.Start ("direct");
-- To invoke this function in a goroutine, use `go f(s)` . This new goroutine will
-- execute concurrently with the calling one.
G2.Start ("goroutine");
-- You can also start a goroutine for an anonymous function call.
G3.Start ("going");
-- Our two function calls are running asynchronously in separate goroutines now.
-- Wait for them to finish (for a more robust approach, use a `WaitGroup` ).
Sleep(1.0);
Put_Line ("done");
end Goroutines;
When we run this program, we see the output of the blocking call first, then the output of the two goroutines. The goroutines’ output may be interleaved, because goroutines are being run concurrently by the runtime.
$ ./goroutines
direct : 0
direct : 1
direct : 2
goroutine : 0
going
goroutine : 1
goroutine : 2
done
Next we’ll look at a complement to goroutines in concurrent programs: channels.