初始gin框架
154字小于1分钟
2025-02-04
首先是安装第三方库
go get github.com/gin-gonic/gin
写gin程序一套固定的格式
- 初始化
- 写路由
- 监听运行
package main
import (
"github.com/gin-gonic/gin"
)
type Respose struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
func Index(c *gin.Context) {
c.JSON(200, Respose{Code: 200, Msg: "成功", Data: map[string]string{}})
}
func main() {
//关闭gin默认的那些debug输出
gin.SetMode("release")
//1. 初始化
r := gin.Default()
//2. 路由
r.GET("/", Index)
//3. 绑定端口运行
r.Run(":8081")
}
关闭debug输出
gin.SetMode("release")
配置端口IP让其他人访问
r.Run(":8080") // 等价于 r.Run("0.0.0.0:8080")