在使用 Go 的 html/template 包渲染 HTML 文件时,默认会使用文件名作为模板名称,而不考虑文件后缀。如果需要设置模板名称的后缀为 '.html',可以通过以下方式实现:

在创建模板时,使用 template.ParseFiles 函数指定模板文件的路径,并在文件名后面添加 '.html' 后缀,例如:

tmpl, err := template.ParseFiles('path/to/template.html')

在渲染模板时,使用 template.ExecuteTemplate 函数指定模板名称时,也需要在模板名称后面添加 '.html' 后缀,例如:

err := tmpl.ExecuteTemplate(w, 'template.html', data)

通过这种方式,就可以将模板名称的后缀设置为 '.html',从而正确地渲染 HTML 文件。

Go HTML/Template 设置模板名称后缀为 .html

原文地址: https://www.cveoy.top/t/topic/nSCB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录