Google's Go language is a new language. Therefor I was surprised to find that it has a 'goto' statement. I've always been taught that 'goto' statements are a thing of the past and evil for it occludes the actual flow of a program. Function (or methods if you will) are always a better way of controlling flow.

I must be missing something. Why and when is using 'goto' a good idea? Or why did Google include it?

share|improve this question
2  
There are times where you really need a goto statement. Goto's are evil only when used indiscriminately. For example, if it is very difficult, if not impossible, to write a Finite state machine parser withou goto statements. – xbonez Jun 16 '12 at 16:02
3  
It's not specific to Go, but for a good discussion on why languages retain the statement, and to see arguments against its use, check out this post. There are some good references linked in the question. Edit: here's another. – Cᴏʀʏ Jun 16 '12 at 16:05
    
To save the OP from grepping through the provided SO discussions, here's the discussion on LKML which pretty much sums it up why goto is useful in certain cases. Read after studying @Kissaki's answer. – kostix Jun 16 '12 at 18:17
    
Related: programmers.stackexchange.com/q/566/33478 (and see my answer). – Keith Thompson Dec 16 '14 at 23:45
up vote 45 down vote accepted

When we actually check Gos source code (the standard library), we can see where gotos are actually well applied.

For example, in the math/gamma.go file, the goto statement is used:

  for x < 0 {
    if x > -1e-09 {
      goto small
    }
    z = z / x
    x = x + 1
  }
  for x < 2 {
    if x < 1e-09 {
      goto small
    }
    z = z / x
    x = x + 1
  }

  if x == 2 {
    return z
  }

  x = x - 2
  p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6]
  q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7]
  return z * p / q

small:
  if x == 0 {
    return Inf(1)
  }
  return z / ((1 + Euler*x) * x)
}

The goto in this case saves us from introducing another (boolean) variable used just for control-flow, checked for at the end. In this case, the goto statement makes the code actually better to read and easier follow (quite in contrary to the argument against goto you mentioned).

Also note, that the goto statement has a very specific use-case. The language specification on goto states that it may not jump over variables coming into scope (being declared), and it may not jump into other (code-)blocks.

share|improve this answer
17  
Without goto, that code would've been less readable, or at least longer. That will surprise brainwashed Java programmers. – Moshe Revah Jun 22 '12 at 10:25
33  
In your example, why not just introduce a function small(x,z) to call instead? That way we don't have to think about what variables are accessible in the small: label. I suspect the reason is go still lacks certain types of inlining support in the compiler. – Thomas Ahle Jun 2 '13 at 12:11
2  
@Jessta: That's what we've got visibility and scope for, right? – Thomas Ahle Jul 11 '14 at 0:13
5  
@ThomasAhle Go does not allow goto to point to a label after new variables have been introduced. Executing the "goto" statement must not cause any variables to come into scope that were not already in scope at the point of the goto. – ogc-nick Sep 11 '14 at 17:50
2  
@ogc-nick Sorry I wasn't clear, I meant the functions can be declared in the scope where they are needed, so they aren't visible to code that doesn't need them. I wasn't talking about goto's and scope. – Thomas Ahle Sep 11 '14 at 23:09

As far as I can tell, goto is mainly used to implement giant state machines inside a single function. Modern software development tends to implement state machines by splitting them into classes and multiple methods, but some people don't trust the compiler to optimize their code so they want to manually inline everything into one giant function.

share|improve this answer
    
Sometimes, in game development, even a Function Call is huge overhead. When you care about RAW performance where single Stack Frame will incur huge performance overheads, and you are indeed implementing FSMs, or you have XYZ loop and you want to break it from inside, goto is your only friend. – Петър Петров Jan 25 '17 at 12:37
    
Yes, but a good compiler should do the optimization for you. Sadly, Go's compiler favors compilation speed over optimization. – Antimony Jan 25 '17 at 17:08
    
Unfortunately, often, compilers don't like us, especially at the inline level. Macros remains the best solution... and goto remains one of the important main features. If you try to port a C library, you will often decide to keep its gotos around even in modern languages. And if you write language translator, like, IL2CPP, goto is the only tool remaining to you for easy branching implementations. [well, such generated code remains ugly bu it's not intended for humans to read] – Петър Петров Jan 26 '17 at 21:22

Goto statements has received a lot of discredit since the era of Spaghetti code in the 60s and 70s. Back then there was very poor to none software development methodology. However Goto are not natively evil but can of course be misused and abused by lazy or unskilled programmers. Many problems with abused Gotos can be solved with development processes such as team code reviews.

goto are jumps in the same technical manner as continue, break and return. One could argue that these are statements are evil in the same manner but they are not.

Why the Go team has included Gotos are probably because of the fact that it is a common flow control primitive. Additionally they hopefully concluded that the scope of Go excludes making an idiot-safe language not possible to abuse.

share|improve this answer

Goto is a good idea when none of the built-in control features do quite what you want, and when you can express what you want with a goto. (It's a shame in these cases in some languages when you don't have a goto. You end up abusing some control feature, using boolean flags, or using other solutions worse than goto.)

If some other control feature (used in a reasonably obvious way) can do what you want, you should use it in preference to goto. If not, be bold and use goto!

Finally it's worth noting that Go's goto has some restrictions designed to avoid some obscure bugs. See these restrictions in the spec.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.