Concurrent Goroutines in Golang

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge is designed to test your understanding of goroutines in Golang. Goroutines are lightweight threads of execution that allow for concurrent execution of functions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/ConcurrencyGroup(["`Concurrency`"]) go/ConcurrencyGroup -.-> go/goroutines("`Goroutines`") subgraph Lab Skills go/goroutines -.-> lab-15399{{"`Concurrent Goroutines in Golang`"}} end

Goroutines

The problem to be solved in this challenge is to create and run goroutines to execute functions concurrently.

Requirements

  • The f function should print out its input string and a counter variable three times.
  • The main function should call the f function synchronously and print out "direct" and a counter variable three times.
  • The main function should call the f function asynchronously using a goroutine and print out "goroutine" and a counter variable three times.
  • The main function should start a goroutine to execute an anonymous function that prints out a message.
  • The main function should wait for the goroutines to finish executing before printing out "done".

Example

## 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
## Go runtime.
$ go run goroutines.go
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 Go programs: channels.

Summary

In this challenge, you learned how to create and run goroutines to execute functions concurrently. You also learned how to start a goroutine to execute an anonymous function and how to wait for goroutines to finish executing.

Other Go Tutorials you may like