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.

33 lines
810 B

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