You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"fmt"
"log"
"net/http"
)
/**
从golang的异常机制可以看出异常处理recover最好是在接口的入口处
就将其插入到对应defer队列中。这样在接口调用过程中即使发生异常
程序依然可以提供服务。
https://studygolang.com/articles/14168
TODO
(1)在每个Grpc的接口入口处加上defer的recover异常处理以gin的方式返回错误但主进程不会崩溃。
(2)修改一下代码生成器,这样就不用每个接口都手动去写这个异常处理了,因为代码都是一样的。
*/
func HelloServer(w http.ResponseWriter, req *http.Request) {
//在可能出现异常的函数调用前使用defer进行加入defer队列即使出现异常也不会整个进程退出~
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
//调用函数
var MakecoreData *int = nil
*MakecoreData = 10000
fmt.Fprintf(w, "hello world")
}
func main() {
http.HandleFunc("/hello", HelloServer)
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}