Различия между сопрограммами и 'goto'?

Если Вы разрабатываете свой проект с Муравьем, можно присвоить ярлык на "Выполнения последний запущенный внешний Инструмент" как Ctrl+Enter, и он повторит последнюю сборку. Это намного легче, чем стандартный Alt+Shift+X, Q также это помогает с ошибкой в последнем Eclipse, который не может найти файл типа "build" муравья в проекте.

11
задан John Weldon 5 October 2017 в 18:38
поделиться

2 ответа

Горутины - это не то же самое, что goto - они работают параллельно с основным кодом. Когда вы указываете что-то вроде (из их примера на http://golang.org/doc/effective_go.html )

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

, основной код продолжается - он не ждет завершения сортировки. Подпрограмма сортировки запускается в своем собственном облегченном потоке выполнения, и когда она завершает сортировку, этот поток завершает работу.

Goto приведет к переходу основного кода на отдельный путь выполнения - поэтому операторы после goto никогда не запустится.

18
ответ дан 3 December 2019 в 03:35
поделиться

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? ;-)

9
ответ дан 3 December 2019 в 03:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: