2015-08-14 110 views
10

基本信息相同的代碼,但不同的結果去模板

  • 轉到版本:go1.4.2達爾文/ AMD64
  • 操作系統:Mac OS X 10.10.5

我正在致力於一個基於go杜松子酒編寫的小型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有2個額外的雙引號。

爲什麼會發生這種情況?以及如何解決這個問題?

回答

相關問題