Suppose we have a subroutine call f(s). Here’s how we’d call that in the usual way, running it synchronously.
To invoke this subroutine in a goroutine-like fashion, we simulate asynchronous behavior by calling it within another subroutine.
You can also start a subroutine for an anonymous function call.
Our two subroutine calls are running asynchronously now. We use a sleep subroutine to wait for them to finish (for a more robust approach, use proper concurrency control).
When we run this program, we see the output of the blocking call first, then the output of the simulated goroutines. The outputs may be interleaved because they are being run concurrently in our simulation.
Next, we’ll look at a complement to goroutines in concurrent programs: channels.