亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我嘗試從 Go net/http 轉換為 Go Fiber,如何進行轉換?

我嘗試從 Go net/http 轉換為 Go Fiber,如何進行轉換?

Go
米琪卡哇伊 2022-10-24 10:15:12
我是 golang 的新手,然后我學習了,我喜歡 Go 纖維。我從 Go Fiber 中學習,我看到 net/http 示例非???。然后我嘗試將 Go net/http 示例轉換為 Go 光纖。下面是上網/httppackage mainimport (    "embed"    "io/fs"    "log"    "net/http"    "runtime/pprof")//go:embed nextjs/dist//go:embed nextjs/dist/_next//go:embed nextjs/dist/_next/static/chunks/pages/*.js//go:embed nextjs/dist/_next/static/*/*.jsvar nextFS embed.FSfunc main() {    // Root at the `dist` folder generated by the Next.js app.    distFS, err := fs.Sub(nextFS, "nextjs/dist")    if err != nil {        log.Fatal(err)    }    // The static Next.js app will be served under `/`.    http.Handle("/", http.FileServer(http.FS(distFS)))    // The API will be served under `/api`.    http.HandleFunc("/api", handleAPI)    // Start HTTP server at :8080.    log.Println("Starting HTTP server at http://localhost:8080 ...")    log.Fatal(http.ListenAndServe(":8080", nil))}func handleAPI(w http.ResponseWriter, _ *http.Request) {    // Gather memory allocations profile.    profile := pprof.Lookup("allocs")    // Write profile (human readable, via debug: 1) to HTTP response.    err := profile.WriteTo(w, 1)    if err != nil {        log.Printf("Error: Failed to write allocs profile: %v", err)    }}
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

我認為你可以替換這個:


  app.Get("/api", func(c *fiber.Ctx) error {

    profile := pprof.Lookup("allocs")

    return c.Get(profile)

  })

有了這個:


  app.Get("/api", func(c *fiber.Ctx) error {

    profile := pprof.Lookup("allocs")

    return profile.WriteTo(c, 1)

  })

*fiber.Ctximplements io.Writer,因此您應該能夠像http.ResponseWriter在這種情況下一樣使用它。


查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號