Closures and goroutines

You may have noticed the anonymous goroutine in Lorem Ipsum:

  go func() {
    go capitalize(index, length, letters, &finalIpsum)
  }()

While it isn't always ideal, there are plenty of places where inline functions work best in creating a goroutine.

The easiest way to describe this is to say that a function isn't big or important enough to deserve a named function, but the truth is, it's more about readability. If you have dealt with lambdas in other languages, this probably doesn't need much explanation, but try to reserve these for quick inline functions.

In the earlier examples, the closure works largely as a wrapper to invoke a select statement or to create anonymous goroutines that will feed the select statement.

Since functions ...

Get Go: Building Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.