Finding Unreachable Go Code

Let’s learn how to find unreachable Go code.

We'll cover the following

Sometimes, a wrongly implemented if or a misplaced return statement can create blocks of code that are unreachable, that is, blocks of code that are not going to be executed at all. Because this is a logical kind of error, which means that it is not going to get caught by the compiler, we need to find a way of discovering it.

Coding example

Fortunately, the go vet tool, which examines Go source code and reports suspicious constructs, can help with that—the use of go vet is illustrated with the help of the cannotReach.go source code file, which contains the next two functions:

Get hands-on with 1200+ tech skills courses.