使用第三方组件:"github.com/gin-gonic/gin
demo:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func RootHandlerFunc() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
// Set example variable
c.Set("example", "12345")
// before request
c.Next()
// after request
latency := time.Since(t)
fmt.Println(latency)
}
}
func main() {
router := gin.Default()
v1 := router.Group("/v1", RootHandlerFunc())
{
v1.GET("/login", LoginEndpoint)
}
s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}