4、gin 模板处理

gin 目录结构
// 忽略掉依赖包目录
╰─$ tree -I vendor
.
├── debug
├── main.go
└── templates
    └── index.html

main.go 主文件
package main

import (
	"github.com/gin-gonic/gin"
)

func main() {

	router := gin.Default()
	// 加载 templates 目录下的所有文件
	router.LoadHTMLGlob("templates/*")
	// 或者使用这种方法加载也是OK的: router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")

	router.GET("/index", func(c *gin.Context) {
		// 参数使用 gin.H 传入index.html中!也就是使用的是index.html模板
		c.HTML(200, "index.html", gin.H{
			"title": "Gin 测试模板",
		})
	})
	
		// 重定向
	router.GET("/vic", func(c *gin.Context) {
		// c.Redirect(302, "https://www.baidu.com")
		c.Redirect(302, "index")
	})
	
	
	router.Run(":8000")
}

index.html 模板
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Gin Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
</head>
<body>
    <h1>
        {{.title}}
    </h1>
</body>
</html>
页面显示
// 访问
http://127.0.0.1:8000/index

// 显示
Gin 测试模板
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值