Go Fiber - 我尝试将 Go net/http 转换为 Go Fiber,如何转换?
Go Fiber - I try to translate from Go net/http to Go Fiber, how to cenvert it?
我是 golang 的新手,然后我开始学习并且我喜欢 Go fiber。
我从 Go fiber 学习,我发现 net/http 示例非常酷。
然后我尝试将 Go net/http 示例转换为 Go fiber.
下面是走net/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)
}
}
我改成
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"))
}
但是我得到如下错误:
# 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)
请帮忙验证一下,如何才能成功得到pprof.Lookup("allocs")?
提前致谢
我想你可以替换这个:
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.Ctx
实现了 io.Writer
,因此在这种情况下您应该可以像 http.ResponseWriter
一样使用它。
我是 golang 的新手,然后我开始学习并且我喜欢 Go fiber。 我从 Go fiber 学习,我发现 net/http 示例非常酷。 然后我尝试将 Go net/http 示例转换为 Go fiber.
下面是走net/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)
}
}
我改成
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"))
}
但是我得到如下错误:
# 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)
请帮忙验证一下,如何才能成功得到pprof.Lookup("allocs")? 提前致谢
我想你可以替换这个:
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.Ctx
实现了 io.Writer
,因此在这种情况下您应该可以像 http.ResponseWriter
一样使用它。