When using the gin.IRouter interface, do i have to do a type assertion from gin.IRouter -> *gin.Engine
to call the Run function?
I dont have that big of a problem doing it, i just wanted to know if that is typical Go way of doing it.
Comment From: ljluestc
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
// Create a gin.Engine instance
router := gin.Default()
// Register a simple handler
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
// Pass the router as a gin.IRouter (example of interface usage)
var iRouter gin.IRouter = router
// Type assertion to *gin.Engine to call the Run function
engine, ok := iRouter.(*gin.Engine)
if !ok {
panic("Failed to assert gin.IRouter to *gin.Engine")
}
// Start the server
engine.Run(":8080")
}