gin框架
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) { //配置路由
c.String(200, "%v", "你好") //状态码和格式化输出的内容
})
r.POST("/add", func(c *gin.Context) {
c.String(200, "123")
})
r.PUT("/put", func(c *gin.Context) {
c.String(http.StatusOK, "123")
})
r.Run(":8000")
}
同时可以使用热加载的工具辅助
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Article struct {
Title string
Desc string
Content string
}
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, map[string]interface{}{ //json数据
"success": true,
"msg": "hasd",
})
})
r.GET("/json2", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{ //别名
"success": true,
"msg": "hasd",
})
})
r.GET("/json", func(c *gin.Context) {
a := &Article{
Title: "123",
Content: "1234",
Desc: "545",
}
c.JSON(http.StatusOK, a)
})
r.GET("/jsonp", func(c *gin.Context) { //处理跨域
a := &Article{
Title: "123",
Content: "1234",
Desc: "545",
}
c.JSONP(http.StatusOK, a)
})
r.GET("/xml", func(c *gin.Context) {
c.XML(http.StatusOK, gin.H{ //json数据
"success": true,
"msg": "hasd",
})
})
r.GET("/news", func(c *gin.Context) {
c.HTML(http.StatusOK, "news.html", gin.H{ //json数据
"success": true,
"msg": "hasd",
})
})
r.Run()
}
json,jsonp,xml,HTML等数据的加载,注意需要渲染模板r.LoadHTMLGlob("templates/*")
r.LoadHTMLGlob("templates/**/*")
**表示一层目录,*表示所有文件,这时候需要给html文件起个别名
{{define "xxxxx"}}
{{end}}
输出变量通过{{.xxx}}
定义变量输出变量
{{$t:=.success}}
{{$t}}
if条件判断
range遍历
with解构数据,直接通过.访问
自定义模板函数
package main
import (
"net/http"
"text/template"
"time"
"github.com/gin-gonic/gin"
)
type Article struct {
Title string
Desc string
Content string
}
func UnixToTime(timestamp int) string {
t := time.Unix(int64(timestamp), 0)
return t.Format("2006-01-02 15:04:05")
}
func main() {
r := gin.Default()
r.SetFuncMap(template.FuncMap{
"UnixToTime": UnixToTime,
})
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, map[string]interface{}{ //json数据
"success": true,
"msg": "hasd",
})
})
r.GET("/json2", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{ //别名
"success": true,
"msg": "hasd",
})
})
r.GET("/json", func(c *gin.Context) {
a := &Article{
Title: "123",
Content: "1234",
Desc: "545",
}
c.JSON(http.StatusOK, a)
})
r.GET("/jsonp", func(c *gin.Context) { //处理跨域
a := &Article{
Title: "123",
Content: "1234",
Desc: "545",
}
c.JSONP(http.StatusOK, a)
})
r.GET("/xml", func(c *gin.Context) {
c.XML(http.StatusOK, gin.H{ //json数据
"success": true,
"msg": "hasd",
})
})
r.GET("/news", func(c *gin.Context) {
c.HTML(http.StatusOK, "news.html", gin.H{ //json数据
"success": true,
"msg": "hasd",
"date": 1741746247,
})
})
r.Run()
}
<body>
<h2>
{{UnixToTime .date}}
</h2>
</body>
注意要写在加载模板之前
引入外部公共模板
引入静态文件
get方法传参获取
r.GET("/get", func(c *gin.Context) {
username := c.Query("username")
age := c.Query("age")
page := c.DefaultQuery("page", "1")
c.JSON(http.StatusOK, map[string]interface{}{ //json数据
"success": username,
"age": age,
"page": page,
})
})
r.POST("/post", func(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
c.JSON(http.StatusOK, map[string]interface{}{ //json数据
"username": username,
"password": password,
})
})
获取post传参
r.POST("/post", func(c *gin.Context) {
username := c.PostForm("username")
password := c.DefaultPostForm("password", "213")
c.JSON(http.StatusOK, map[string]interface{}{ //json数据
"username": username,
"password": password,
})
})
获取get post传递的数据绑定到结构体
type UserInfo struct {
Username string `json:"username" form:"username"`
Password string `json:"password" form:"password"` //json表示把返回的进行小写,后边匹配的表单
}
r.GET("/getUser", func(c *gin.Context) {
user := &UserInfo{}
if err := c.ShouldBind(&user); err == nil {
c.JSON(http.StatusOK, user)
} else {
c.JSON(http.StatusOK, gin.H{
"err": err.Error(),
})
}
})
xml
type AArticle struct {
Title string `json:"title" xml:"title"`
Content string `json:"password" xml:"content"`
}
r.POST("/postXml", func(c *gin.Context) {
xmlSliceData, _ := c.GetRawData()
article := &AArticle{}
if err := xml.Unmarshal(xmlSliceData, &article); err == nil {
c.JSON(http.StatusOK, article)
} else {
c.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
}
})
动态路由
r.GET("/list/:cid", func(c *gin.Context) {
cid := c.Param("cid")
c.String(200, "%v", cid)
})
路由分组
apiRouters := r.Group("/api")
{
apiRouters.GET("/", func(c *gin.Context) {
c.String(200, "asdasd")
})
apiRouters.GET("/sad", func(c *gin.Context) {
c.String(200, "sad")
})
}