当前位置:网站首页>Gin blog 总结1
Gin blog 总结1
2022-06-10 13:39:00 【tnan2522】
gin 是一个基于go http 包上封装的一个web框架, 博客dome展示 www.tnan.top/blog/
模板函数
在gin中也是可以像jinja2一样,自定义模板函数的, 主要用于封装模板时 对模板变量的操作,如将两个模板变量相加, 模板变量字符串切割等操作
app := gin.Default()
//添加自定义的模板函数
app.SetFuncMap(template.FuncMap{
"stringadd": uitls.StringAdd,
"timecut": uitls.TimeCut,
"safe": uitls.Safe,
"truestring": uitls.TrueString,
"urladd": uitls.UrlAdd,
})
在gin 中如果HTML渲染时的数据是HTML标签数据的话,那么它会将这些数据当做字符串来对待,会在这些数据用 " " 进行渲染, 这时候就需要用到template 中的 template.HTML 进行转换了,因为gin 中没有像jinja2 模板引擎的 safe 函数,所以,可以自定义一个 safe 模板函数
自定义404处理函数
在web开发中,404是常见的一个问题,当出现404时,gin 默认会返回 404 page not found
这个返回页面并不好看,我们可以使用 NoRoute
app := gin.Default()
//自定义404处理函数
app.NoRoute(controllers.NoResponse)
自定义处理返回404 函数
注册HTML和css等静态文件路径
//注册html目录
app := gin.Default()
app.LoadHTMLGlob("templates/*") // 会从当前路径下找templates 文件夹
//注册静态资源目录
app.Static("static", "./static")
在处理函数 返回 时会 从 LoadHTMLGlob注册的路径下找 html文件, 如

此时,details.html 会到注册路径下找
静态文件static app.Static("static", "./static")第一个 static是当遇到以 static 开头的请求的时候就将它视为 获取静态文件的请求, 第一个 static可以看做是 路径请求 api , 第二个 ./static是当遇到静态文件请求的时候去哪里获取这个文件,./static是当前路径下的 static 目录
边栏推荐
- SnackBar usage details
- 解决VMWareStation安装 tools 时 D:\setup.exe 找不到的问题
- buuctf [Jupyter]notebook-rce
- Qualcomm has finally begun to develop its own core architecture after learning from the difficulties of assembling chips to maintain its competitive advantage
- Record common functions in MySQL at work
- CL210OpenStack操作的故障排除--常见核心问题的故障排除
- Tablayout usage details (modify text size, underline style, etc.)
- Mmdetection adds precision to the evaluation index
- 3. web page development tool vs Code
- Multithreading killer ---countdownlatch & cyclicbarrier
猜你喜欢

2022大厂高频软件测试面试真题(附答案)

【笔记】关于keil中的出现的编译映射内存不足的问题

Multithreading killer ---countdownlatch & cyclicbarrier

解决win10虚拟机和主机不能互相粘贴复制的问题

MMdetection增加评估指标precision

软件智能:aaas系统 度量衡及文法的形式规则

智慧校园安全通道及视频监控解决方案

【C语言】指针函数与函数指针、数组函数

【无标题】音频蓝牙语音芯片,WT2605C-32N实时录音上传技术方案介绍

解决安装gerapy的时候报错:ERROR: Cannot uninstall ‘certifi‘. It is a distutils installed project...
随机推荐
Bottomnavigationview is used in conjunction with viewpager, to modify icon size, to remove text, etc
Pychart installation tutorial
Kotlin练手,以登录为例,Anko简单使用
Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集
【无标题】
格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
Docker部署一个Redis集群
解决VMware Workstation安装VMware Tools显示灰色的办法
Kotlin practises. Take login as an example. Anko simply uses it
CL210OpenStack操作的故障排除--常见核心问题的故障排除
[raise bar C #] how to call the base of the interface
Z-Wave ecosystem status report in 2022
10 competitive airpods Pro products worth your choice
Ten easy-to-use cross browser testing tools to share, good things worth collecting
解决win10虚拟机和主机不能互相粘贴复制的问题
In depth analysis of "circle group" relationship system design | series of articles on "circle group" technology
[Huang ah code] teacher, I want to choose software development related majors after the college entrance examination. Which direction do you think is better? How to fill in the college entrance examin
5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
移动app性能测试有哪些需要进行?性能测试报告如何收费?
如何定位游戏发热问题