gpt4 book ai didi

html - 使用 gin + go-template 的代码相同但结果不同

转载 作者:IT老高 更新时间:2023-10-28 13:09:33 25 4
gpt4 key购买 nike

基本信息

  • Go 版本:go1.4.2 darwin/amd64
  • 操作系统:Mac OS X 10.10.5

我正在开发一个基于 gogin 编写的小型 Web 项目。这是我的golang代码。运行 go run test.go 后,我们有一个 Web 服务器,它正在监听 8089。

Golang test.go

package main

import "github.com/gin-gonic/gin"
import "net/http"

func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"scheme": "http",
"domain": "meican.loc",
})
})
router.Run(":8089") // listen and serve on 0.0.0.0:8089
}

后端生成的 html 代码应包含前端 javascript 引擎(假设为 Angular.js)使用的模板。

所以模板代码在script标签中,就像这样:

部分模板/index.html

<script type="text/template" charset="utf-8">
<div data="{{.scheme}}://{{.domain}}/qr"></div>
<div data="{{.scheme}}://{{.domain}}/qr"></div> <!-- problem here -->
</script>

当第二次使用 {{.domain}} 时,我得到了不同的结果。我刷新了浏览器并查看了源代码。然后我得到了这个:

浏览器源代码结果

<script type="text/template" charset="utf-8">
<div data="http://meican.loc/qr"></div>
<div data="http://"meican.loc"/qr"></div> <!-- problems here -->
</script>

第二个 div 有两个额外的双引号。

为什么会这样?以及如何解决这个问题?

最佳答案

这是 Go 中的一个错误,并且一直是 slated to be fixed in 1.7截至 2016 年 3 月(在 1.6 中也有 partially addressed)

关于html - 使用 gin + go-template 的代码相同但结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com