简单gin框架web服务器例子
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func Index(context *gin.Context) {
context.String(http.StatusOK, "hello Index")
}
func Home(context *gin.Context) {
type ReturnData struct {
Code int `json:"code"`
Msg string `json:"msg"`
PassWord string `json:"-"` // 忽略转成json
}
data := ReturnData{200, "成功", "123456"}
context.JSON(http.StatusOK, data)
//dataMap := map[string]any{
// "code": 200,
// "msg": "成功111",
//}
//context.JSON(http.StatusOK, dataMap)
//context.JSON(http.StatusOK, gin.H{"code":2000,"msg":"成功"})
}
func main() {
//创建一个默认路由
router := gin.Default()
router.GET("/index", Index)
router.GET("/home", Home)
_ = router.Run("127.0.0.1:8000")
}