views:

442

answers:

2

I always read about the horrible thing that "goto" is. But, todaym reading about the google programming language "Go" http://golang.org/ and i see that it suports Coroutines (Goroutines).

The question is:

Coroutine == GoTo

Or

Coroutine != GoTo?

Why?

+14  A: 

Goroutines are not the same as a goto - they run in parallel with the main code. When you state something like (from their example at http://golang.org/doc/effective%5Fgo.html)

go list.Sort();  // run list.Sort in parallel; don't wait for it.

the mainline code continues on - it doesn't wait for the sort to finish. The sort routine starts up on its own lightweight thread of execution and when it finishes the sort that thread exits.

A goto would cause the mainline code to branch to a separate execution path - so the statements after the goto would never get run.

Aaron
+1 Thanks. 'go' seems like a bad keyword choice, perhaps spinup or task?
kenny
+3  A: 

The key difference is that goto statements in languages that support them allow jumping to any location in the program with little or no restriction. While coroutines may on the surface seem similar they are very different.

Coroutines allow procedures to be suspended (with all their context) and resumed at certain locations. So while coroutines do pause and yield control to other procedures before they complete and then resume later, the points at which the procedures yield and resume from is known ahead of time.

It is not possible to simply jump to an arbitrary line in a procedure, the procedure in question has to waiting to be resumed at a specific location. While this passing of control is much more structured than with goto it is possible to write confusing code by overusing this powerful mechanism. Then again that is that not the case with every powerful programming language feature? ;-)

Tendayi Mawushe