golang 将 fasthttp 替换为 gin,切片边界超出范围运行时错误
golang replace fasthttp to gin with slice bounds out of range runtime error
查看 httpserver 并查看是否可以从 fasthttp 更改为 gin,但在从中间件路由期间卡住并出现运行时错误。如果可能的话,我会尽量使代码彼此相似。
main.go
func main() {
...
route.InitRouters()
/*
s := &fasthttp.Server{Handler: middleware.Handler} // fasthttp
s.ListenAndServe(":8081")
*/
router := gin.New() // gin
router.Use(middleware.Handler)
router.Run("localhost:8180")
...
}
route.go
var router1 *gin.Engine // fasthttp: *fasthttprouter.Router
var router2 *gin.Engine
func InitRouters() {
router1 = gin.New() // fasthttp: fasthttprouter.New()
...
router2 = gin.New()
...
}
func GetRouter1() *gin.Engine { // fasthttp: *fasthttprouter.Router
return router1 // runtime error
}
func GetRouter2() *gin.Engine {
return router2 // runtime error
}
...
middleware.go
...
func Handler(ctx *gin.Context) { // fasthttp: ctx *fasthttp.RequestCtx
if ... {
route.GetRouter1().HandleContext(cts) // fasthttp: route.GetRouter1().Handler(ctx)
} else {
route.GetRouter2().HandleContext(ctx)
}
}
运行时错误发生在 route.go
的 return router1
或 return router2
。
runtime error: slice bounds out of range [:1] with capacity 0
...
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165 (0xc4eaca)
(*Context).Next: c.handlers[c.index](c)
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/recovery.go:99 (0xc62bcc)
CustomRecoveryWithWriter.func1: c.Next()
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165 (0xc4eaca)
(*Context).Next: c.handlers[c.index](c)
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:525 (0xc59777)
serveError: c.Next()
...
我怀疑 main.go
中的 router
无法通过 middleware
将 ctx
路由到 router1
或 router2
。我是否需要使用 &http.Server
而不是 &fasthttp.Server
和 ServeHttp
middleware
处理程序?这通常如何以杜松子酒的方式完成?
对组使用单个引擎:
router = gin.New()
group1:=router.Group("/path1")
group2:=router.Group("/path2")
然后分别配置两个组。
查看 httpserver 并查看是否可以从 fasthttp 更改为 gin,但在从中间件路由期间卡住并出现运行时错误。如果可能的话,我会尽量使代码彼此相似。
main.go
func main() {
...
route.InitRouters()
/*
s := &fasthttp.Server{Handler: middleware.Handler} // fasthttp
s.ListenAndServe(":8081")
*/
router := gin.New() // gin
router.Use(middleware.Handler)
router.Run("localhost:8180")
...
}
route.go
var router1 *gin.Engine // fasthttp: *fasthttprouter.Router
var router2 *gin.Engine
func InitRouters() {
router1 = gin.New() // fasthttp: fasthttprouter.New()
...
router2 = gin.New()
...
}
func GetRouter1() *gin.Engine { // fasthttp: *fasthttprouter.Router
return router1 // runtime error
}
func GetRouter2() *gin.Engine {
return router2 // runtime error
}
...
middleware.go
...
func Handler(ctx *gin.Context) { // fasthttp: ctx *fasthttp.RequestCtx
if ... {
route.GetRouter1().HandleContext(cts) // fasthttp: route.GetRouter1().Handler(ctx)
} else {
route.GetRouter2().HandleContext(ctx)
}
}
运行时错误发生在 route.go
的 return router1
或 return router2
。
runtime error: slice bounds out of range [:1] with capacity 0
...
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165 (0xc4eaca)
(*Context).Next: c.handlers[c.index](c)
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/recovery.go:99 (0xc62bcc)
CustomRecoveryWithWriter.func1: c.Next()
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165 (0xc4eaca)
(*Context).Next: c.handlers[c.index](c)
go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:525 (0xc59777)
serveError: c.Next()
...
我怀疑 main.go
中的 router
无法通过 middleware
将 ctx
路由到 router1
或 router2
。我是否需要使用 &http.Server
而不是 &fasthttp.Server
和 ServeHttp
middleware
处理程序?这通常如何以杜松子酒的方式完成?
对组使用单个引擎:
router = gin.New()
group1:=router.Group("/path1")
group2:=router.Group("/path2")
然后分别配置两个组。