- Go Systems Programming
- Mihalis Tsoukalos
- 240字
- 2021-07-02 18:08:04
Unreachable code
Unreachable code is code that can never be executed and is a logical kind of error. As the Go compiler itself cannot catch such logical errors, you will need to use the go tool vet command to help.
The example code in this section is saved as cannotReach.go and can be divided into two parts.
The first part has the following Go code:
package main import ( "fmt" )
func x() int {
return -1 fmt.Println("Exiting x()") return -1 } func y() int { return -1 fmt.Println("Exiting y()") return -1 }
The second part is as follows:
func main() { fmt.Println(x()) fmt.Println("Exiting program...") }
As you can see, the unreachable code is in the first part. Both x() and y() functions have unreachable code because their return statements were put at the wrong place. However, we are not done yet because we will have to let the go tool vet tool discover the unreachable code. The process is simple and includes the execution of the following command:
$ go tool vet cannotReach.go cannotReach.go:9: unreachable code cannotReach.go:14: unreachable code
Additionally, you can see that go tool vet detects unreachable code even if the surrounding function is not going to be executed at all, as happens with y().
- PHP動態網站程序設計
- 一步一步學Spring Boot 2:微服務項目實戰
- Learning Cython Programming(Second Edition)
- Building a Home Security System with Raspberry Pi
- .NET 4.0面向對象編程漫談:基礎篇
- Software Testing using Visual Studio 2012
- C++程序設計基礎教程
- Java設計模式及實踐
- Learning Python Design Patterns
- 零基礎學單片機C語言程序設計
- INSTANT Sinatra Starter
- NetBeans IDE 8 Cookbook
- Machine Learning in Java
- 軟件測試教程
- Python機器學習之金融風險管理