- 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().
- 玩轉Scratch少兒趣味編程
- Game Programming Using Qt Beginner's Guide
- PHP程序設計(慕課版)
- Java游戲服務器架構實戰
- 數據結構習題精解(C語言實現+微課視頻)
- 高級C/C++編譯技術(典藏版)
- Java設計模式及實踐
- Mastering Predictive Analytics with Python
- Mastering ROS for Robotics Programming
- 批調度與網絡問題的組合算法
- Create React App 2 Quick Start Guide
- Fast Data Processing with Spark(Second Edition)
- Visual Basic程序設計(第三版)
- 快樂編程:青少年思維訓練
- Java Web動態網站開發(第2版·微課版)