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队列中。这样在接口调用过程中,即使发生异常,
程序依然可以提供服务。
*/
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)