一,目录结构:
二,代码:
1,controller/ImageController.go
package controllerimport ("github.com/gin-gonic/gin""net/http"
)type ImageController struct{}func NewImageController() ImageController {return ImageController{}
}//得到详情
func (ic *ImageController) Detail(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"message": "image detail",})
}//得到列表
func (ic *ImageController) List(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"message": "image list",})
}
2,routes/routes.go
package routesimport ("github.com/gin-gonic/gin""imagebank/controller"
)func Routes() *gin.Engine {router := gin.Default()//imageimage := controller.NewImageController()imageGroup := router.Group("/image"){imageGroup.GET("/detail", image.Detail)imageGroup.GET("/list", image.List)}return router
}
3,main.go
package mainimport ("imagebank/routes"
)// 入口函数
func main() {//引入路由r := routes.Routes()//runr.Run(":8080")
}
三,测试效果: