37 lines
631 B
Go
37 lines
631 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
//定义一个中间件程序计时,执行函数后应该打印的执行时间
|
||
|
|
||
|
func timeCal(c *gin.Context) {
|
||
|
start := time.Now()
|
||
|
c.Next()
|
||
|
//统计时间
|
||
|
since := time.Since(start)
|
||
|
fmt.Println("程序耗时", since)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
r := gin.Default()
|
||
|
r.Use(timeCal)
|
||
|
shopping := r.Group("/shopping")
|
||
|
{
|
||
|
shopping.GET("/index", shopIndexHandler)
|
||
|
shopping.GET("/home", shophomeHandler)
|
||
|
}
|
||
|
r.Run(":8000")
|
||
|
}
|
||
|
|
||
|
func shopIndexHandler(c *gin.Context) {
|
||
|
time.Sleep(time.Second * 5)
|
||
|
}
|
||
|
|
||
|
func shophomeHandler(c *gin.Context) {
|
||
|
time.Sleep(time.Second * 3)
|
||
|
}
|