顶级目录 gin
1.bootstrap/route.go 路由 在这里注册
package bootstrap
import (
"gin/routes"
"github.com/gin-gonic/gin"
)
func SetupRoute(r *gin.Engine) {
routes.RegisterAPIRoutes(r)
}
2.routes/route.go 具体路由
package routes
import (
"fmt"
"github.com/gin-gonic/gin"
)
func RegisterAPIRoutes(r *gin.Engine) {
v1 := r.Group("/v1")
v1.GET("hello", func(ctx *gin.Context) {
fmt.Println("helloword")
})
}
3.将 注册的路由 引入到 main.go
package main
import (
"fmt"
"gin/bootstrap"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
bootstrap.SetupRoute(r)
err := r.Run(":8080")
if err != nil {
fmt.Println(err.Error())
}
}
go run main.go
curl 127.0.0.1:8080/v1/hello
结果:helloword
|