Home > Articles > Programming > C/C++

Go for Objective-C Programmers, Part 3

  • Print
  • + Share This
David Chisnall wraps up his three-part series on Go for Objective-C programmers by looking at the feature that makes Go so powerful: concurrency support.
From the author of

So far in this quick tour of Go for Objective-C programmers, we've looked at the abstract model and skimmed over the syntax. The most interesting thing about Go, however, is its support for parallel constructs. If you've written Objective-C code in the last few years, you've probably used blocks and libdispatch a lot. We'll conclude this brief series with a look at how to do something similar in Go, as well as other features of Go that aren't present in Objective-C.

Blocks

The block syntax in Objective-C isn't the ugliest imaginable, but it certainly tries hard. In Go, the same syntax is used for functions everywhere, irrespective of whether they're top-level functions or inner functions (blocks, in Objective-C terminology).

In Objective-C, you need to add a qualifier if you want a variable to be mutable in a block, like this:

__block int a = 12;
^(void) { a++; } ();

The Go version is a bit simpler:

a := 12
func () { a++ } ()

Notice that neither of these examples is actually useful—declaring a block just to call it once inline is a crazy thing to do. Blocks are more useful when passed to other functions or executed in the background.

  • + Share This
  • 🔖 Save To Your Account