基本样式测试

This commit is contained in:
2025-11-23 21:48:11 +08:00
parent 86b28ed215
commit a469da2904
8 changed files with 126 additions and 90 deletions

78
main.go
View File

@@ -4,73 +4,43 @@ 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.Static("/static", "./static")
// 加载模板(支持多层目录)
r.LoadHTMLGlob("templates/**/*")
// 路由
r.GET("/", func(c *gin.Context) {
c.String(200, "首页")
})
r.GET("/json", func(c *gin.Context) {
c.JSON(200, map[string]interface{}{
"success": "true",
"msg": "你好gin",
c.HTML(200, "pages/index.html", gin.H{
"Title": "首页",
})
})
r.GET("/json2", func(c *gin.Context) {
c.JSON(200, gin.H{
"success": "true",
"msg": "你好gin",
r.GET("/about", func(c *gin.Context) {
c.HTML(200, "pages/about.html", gin.H{
"Title": "关于我们",
})
})
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("/articles", func(c *gin.Context) {
c.HTML(200, "pages/articles.html", gin.H{
"Title": "文章列表",
"Articles": []map[string]string{
{"Title": "Gin框架入门", "Date": "2025-11-23"},
{"Title": "Go语言最佳实践", "Date": "2025-11-20"},
},
})
})
r.GET("news", func(c *gin.Context) {
c.HTML(200, "news.html", gin.H{
"title": "我是一个后台数据",
r.GET("/contact", func(c *gin.Context) {
c.HTML(200, "pages/contact.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
} // listen and serve on 0.0.0.0:8080
r.Run(":8080")
}