简介
Gin的实现非常简单,准备开始编写属于自己的微服务吧!
创建Gin Engine
通过gin.Default()和gin.New创建Engine,区别在于Default()会默认使用Logger和Recover中间件
// Default returns an Engine instance with the Logger and Recovery middleware already attached.
func Default() *Engine {
debugPrintWARNINGDefault()
engine := New()
engine.Use(Logger(), Recovery())
return engine
}
路由组
路由组可以在api中添加前缀,比如”/api/v1”
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
v1 := router.Group("/api/v1/todos")
{
v1.GET("/", func(ctx *gin.Context) { ctx.JSON(http.StatusOK, "ok") })
}
router.Run(":8080")
}