例如:
```
http.HandleFunc("/", Fun1)
func Fun1(w http.ResponseWriter, r *http.Request) {
_, err := doSomething1()
if err != nil {
fmt.Fprintln("error!")
return
}
doSomething2()
fmt.Fprintln("ok!")
}
```
例如上面的代码中,`fmt.Fprintln("error!")`下面的`return`可以不用吗?
或者说在`doSomething1 `函数中就中断输出,不再执行后面的代码了。
更多评论
func main() {
http.HandleFunc("/",Fun1)
http.ListenAndServe(":5000",nil)
}
func Fun1(w http.ResponseWriter, r *http.Request) {
_, err := doSomething1()
fmt.Println(err)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("error!")
return
}
doSomething2()
w.WriteHeader(http.StatusOK)
}
func doSomething1()(string,error) {
defer func() {
if err := recover(); err != nil {
fmt.Println("Panic info is: ", err)
}
}()
panic("SimplePanicRecover function panic-ed!")
return "abc",errors.New("abc")
}
func doSomething2() {
return
}
#2