給httprouter新增pprof功能

narutoinfo發表於2017-07-18

給httprouter新增pprof

1:獲取包

go get github.com/feixiao/httpprof

2:進入所在目錄,獲取依賴

 govendor sync

3:編譯執行example

go build

4:外部專案新增使用,只需要參考example的使用即可

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

func main() {
    router := httprouter.New()

    // 在原來的httprouter的使用基礎只是新增了這一句程式碼
    router = httpprof.WrapRouter(router)

    router.GET("/", Index)
    router.GET("/hello/:name", Hello)

    log.Fatal(http.ListenAndServe(":8080", router))
}

相關文章