golang:unreachable code
有时候编译器会出现 这样的提示:unreachable code

原因:是因为 出现提示位置的前面,有代码 死循环、panic、或者 return 等,导致代码永远无法执行到目标位置。
案例:
案例1:死循环:
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("hello,world")
time.Sleep(time.Second*2)
}
fmt.Println("hi") // 编译器提示:unreachable code
/*
解决办法就是把你把要调用的方法写到return前面就好了
*/
}
案例2:panic
package main
import "fmt"
func main() {
fmt.Println("hello,world")
panic("Oh,good by") // 编辑器 提示:unreachable code
fmt.Println("hello,world")
}
案例3:return
package main
import "fmt"
func main() {
fmt.Println("hello,world")
return
fmt.Println("hello,world") // 编辑器 提示:unreachable code
}
浙公网安备 33010602011771号