Why does Go have a “goto” statement

前端 未结 3 1976
悲&欢浪女
悲&欢浪女 2020-12-23 13:01

I was surprised to find that Go 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

3条回答
  •  醉梦人生
    2020-12-23 13: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.

提交回复
热议问题