Files
GoCode/main.go
2026-02-21 12:23:56 +08:00

49 lines
1022 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
)
func initMiddleware(c *gin.Context) {
//获取纳秒时间戳
start := time.Now().UnixNano() // 获取纳秒时间戳
fmt.Println("1-我是一个中间件")
c.Next()
fmt.Println("2-我是一个中间件")
end := time.Now().UnixNano()
fmt.Println("耗时:", end-start)
}
// main 函数是程序的入口点初始化并启动Web服务器
func main() {
// 创建默认的gin引擎
r := gin.Default()
// 加载模板文件,支持多层目录结构
r.LoadHTMLGlob("templates/**/*")
// 配置静态文件目录,将./static目录映射到URL路径/static
r.Static("/static", "./static")
//全局中间件
r.Use(initMiddleware)
r.GET("/", func(c *gin.Context) {
fmt.Println("这是一个首页")
time.Sleep(20 * time.Second)
c.String(200, "首页")
})
r.GET("/news", func(c *gin.Context) {
c.String(200, "新闻页")
})
// 启动HTTP服务器监听在8081端口
err := r.Run(":8088")
if err != nil {
return
}
}