gin路由中相应数据
This commit is contained in:
70
main.go
70
main.go
@@ -1,22 +1,74 @@
|
||||
package main
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
Desc string
|
||||
Content string
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
//从服务器请求资源
|
||||
//html模板文件
|
||||
r.LoadHTMLGlob("templates/*")
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.String(200, "这是一个get请求,用于取出资源")
|
||||
c.String(200, "首页")
|
||||
})
|
||||
r.POST("/add", func(c *gin.Context) {
|
||||
c.String(200, "这是一个post--主要用于增加数据")
|
||||
r.GET("/json", func(c *gin.Context) {
|
||||
c.JSON(200, map[string]interface{}{
|
||||
"success": "true",
|
||||
"msg": "你好gin",
|
||||
})
|
||||
r.PUT("/edit", func(c *gin.Context) {
|
||||
c.String(200, "这是一个put请求 主要用于编辑数据")
|
||||
})
|
||||
r.DELETE("/delete", func(c *gin.Context) {
|
||||
c.String(200, "这是一个delete请求 主要用于删除数据")
|
||||
|
||||
r.GET("/json2", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"success": "true",
|
||||
"msg": "你好gin",
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("/json3", func(c *gin.Context) {
|
||||
a := Article{
|
||||
Title: "我是一个标题",
|
||||
Desc: "描述",
|
||||
Content: "测试内容",
|
||||
}
|
||||
c.JSON(200, a)
|
||||
})
|
||||
|
||||
//响应jsonp请求 http://0.0.0.0:8080/jsonp?callback=xxx
|
||||
r.GET("/jsonp", func(c *gin.Context) {
|
||||
a := Article{
|
||||
Title: "我是一个标题",
|
||||
Desc: "描述",
|
||||
Content: "测试内容",
|
||||
}
|
||||
c.JSONP(200, a)
|
||||
})
|
||||
|
||||
r.GET("xml", func(c *gin.Context) {
|
||||
c.XML(200, gin.H{
|
||||
"success": "true",
|
||||
"msg": "你好gin,我是一个xml",
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("news", func(c *gin.Context) {
|
||||
c.HTML(200, "news.html", gin.H{
|
||||
"title": "我是一个后台数据",
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("goods", func(c *gin.Context) {
|
||||
c.HTML(200, "news.html", gin.H{
|
||||
"title": "我是一个商品页面",
|
||||
})
|
||||
})
|
||||
|
||||
err := r.Run()
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
12
templates/goods.html
Normal file
12
templates/goods.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>{{.title}}</h2>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
12
templates/index.html
Normal file
12
templates/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>我是一个新闻界面</h2>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
12
templates/news.html
Normal file
12
templates/news.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>{{.title}}</h2>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user