[英]Go Fiber - I try to translate from Go net/http to Go Fiber, how to cenvert it?
I am new to golang then I learn and I love Go fiber.我是 golang 的新手,然后我学习了,我喜欢 Go 光纤。 I learn from Go fiber and I see that net/http example is so cool.
我从 Go 光纤中学习,我看到 net/http 示例非常酷。 then I try to convert from Go net/http example to Go fiber.
然后我尝试从 Go net/http 示例转换为 Go 光纤。
The below is go net/http下面是 go 网/http
package main
import (
"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/*/*.js
var nextFS embed.FS
func 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)
}
}
I change it to我把它改成
package main
import (
"embed"
"log"
// "io/fs"
"runtime/pprof"
"github.com/gofiber/fiber/v2"
)
//go:embed nextjs/dist
//go:embed nextjs/dist/_next
//go:embed nextjs/dist/_next/static/chunks/pages/*.js
//go:embed nextjs/dist/_next/static/*/*.js
var nextFS embed.FS
func main() {
// Root at the `dist` folder generated by the Next.js app.
app := fiber.New()
// The static Next.js app will be served under `/`.
//http.Handle("/", http.FileServer(http.FS(distFS)))
app.Static("/","./nextjs/dist")
// The API will be served under `/api`.
app.Get("/api", func(c *fiber.Ctx) error {
profile := pprof.Lookup("allocs")
return c.Get(profile)
})
// Start HTTP server at :8080.
log.Println("Starting HTTP server at http://localhost:8080 ...")
log.Fatal(app.Listen(":8080"))
}
but I got error as follow:但我得到如下错误:
# command-line-arguments
./main.go:29:17: cannot use profile (type *pprof.Profile) as type string in argument to c.Get
./main.go:29:17: cannot use c.Get(profile) (type string) as type error in return argument:
string does not implement error (missing Error method)
Please help to verify and how can I get the pprof.Lookup("allocs") successfully?请帮助验证,我怎样才能成功获得 pprof.Lookup("allocs") ? Thanks in advance
提前致谢
I think you can just replace this:我认为你可以替换这个:
app.Get("/api", func(c *fiber.Ctx) error {
profile := pprof.Lookup("allocs")
return c.Get(profile)
})
With this:有了这个:
app.Get("/api", func(c *fiber.Ctx) error {
profile := pprof.Lookup("allocs")
return profile.WriteTo(c, 1)
})
*fiber.Ctx
implements io.Writer
, so you should be able to use it like http.ResponseWriter
in this instance. *fiber.Ctx
实现io.Writer
,所以在这个例子中你应该可以像http.ResponseWriter
一样使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.