You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed this in some badly re-factored complex code, but it reduces nicely into this toy program. Basically the compiler allows unreachable statements that could be detected statically at compile time. It would be nice if code like this failed to compile.
package main
import "fmt"
func main() {
fmt.Println("foo", foo())
}
func foo() int {
fmt.Println("now you see me")
return 0
fmt.Println("now you don't")
return 1
}
The text was updated successfully, but these errors were encountered:
Let me add that to try to ensure that all Go compilers agree on which programs can be compiled, the notion of an unreachable statement is actually defined by the language spec: https://golang.org/ref/spec#Terminating_statements .
Permitting early returns helps debugging a large function where you might
want to quickly disable the last portion by putting in a return.
The alternative of adding an if 0 (which also introduces deadcode) or
commenting out/deleting aren't as easy.
I noticed this in some badly re-factored complex code, but it reduces nicely into this toy program. Basically the compiler allows unreachable statements that could be detected statically at compile time. It would be nice if code like this failed to compile.
The text was updated successfully, but these errors were encountered: