Nothing against gotos, but the cringe from that implementation was way too strong. Why does this even get any attention at all?
I didn't stop and try to reason it out on my own, but then I clicked and saw the answer, and said "heh. cute." So there you go- enough people felt the same way I did, that the voting system moved this to the 4th link on the top page. /shrug. That's the internet for you!
"Prepare to cringe."
It's a dirty hack, this is Hacker News.
goto=42
while true ; do
case $goto in
1 )
code for label 1
goes here
break # finish
;;
42 )
code for label 42
goes here
goto=1
;;
esac
done
You don't have the syntactic sugar here, and there are some inconveniences, but think of the positives: this does not claim a new stack frame for each jump, let a lone a new stack frame holding a big string variable that holds most of the script source code.EDIT: /me sighs, this isn't the one I was looking for but it'll do for now. http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html / http://www.chiark.greenend.org.uk/~sgtatham/coroutines.h