Skip to main content
In this lesson, you’ll learn how to execute functions concurrently in Go using goroutines. By simply prefixing any function call with the go keyword, you can run the function as a separate goroutine.

Sequential Execution Example

The following example demonstrates a sequential program that calculates the square of an integer after a one-second delay:
In this version, each call to calculateSquare is executed one after the other, which accumulates a significant overall delay.

Running Functions Concurrently

To run the same task concurrently, simply add the go keyword before the function call. This launches each call to calculateSquare as a separate goroutine:
When you run this concurrent version, you might see output like:
Although the elapsed time appears very short (e.g., 13 milliseconds), none of the squares are printed. This behavior occurs because the main function completes and exits before the goroutines have finished executing.

Ensuring Goroutines Complete Execution

A simple workaround to allow all goroutines to finish is by adding a delay in the main function using time.Sleep. For example, adding a two-second pause gives the goroutines enough time to complete:
When you run the updated program:
You will see a large number of squares printed in the terminal along with an output similar to:
Using time.Sleep is not the most reliable method to wait for goroutines to complete. A better approach is to use synchronization primitives such as WaitGroups, which will be covered in an upcoming lesson.

Conclusion

Goroutines are a powerful way to enable concurrency in your Go programs by allowing functions to execute concurrently with minimal changes to your code. As you continue, learn to manage synchronization between goroutines efficiently for more robust and production-ready applications. Happy coding, and see you in the next lesson!

Watch Video