Closured Variables and the go Statement

Let’s learn about closured variables and the go statement.

The goClosure.go utility

In this lesson, we are going to talk about closured variables, which are variables inside closures, and the go statement. Notice that closured variables in goroutines are evaluated when the goroutine actually runs and when the go statement is executed in order to create a new goroutine. This means that closured variables are going to be replaced by their values when the Go scheduler decides to execute the relevant code.

Coding example

This is illustrated in the main() function of goClosure.go:

Get hands-on with 1200+ tech skills courses.