2015-06-13 21 views
0

存儲在PostgreSQL中的數據:The <b>Argentine Army</b> is。數據類型:"content" text COLLATE "default"Golang + PostgreSQL - 如何打印確切的查詢沒有轉義的HTML標籤?

當打印通過Golang,它成爲The &lt;b&gt;Argentine Army&lt;/b&gt; is

我需要打印和PostgreSQL確切的數據沒有逃脫的HTML標籤。我不確定這是Go還是PostgreSQL問題。下面

是我Golang代碼:

package main 

import (
    "database/sql" 
    "github.com/labstack/echo" 
    _ "github.com/lib/pq" 
    "html/template" 
    "io" 
    "log" 
    "net/http" 
) 

// type Gallery struct here 
// type Template struct & function here 

func main() { 
    e := echo.New() 

    // DB connection here 
    // Parse template & render here 

    e.Get("/", func(c *echo.Context) error { 

     rows, err := db.Query("SELECT uri, title, content FROM gallery WHERE id=123") 
     // Handle error here 

     gallery := []Gallery{} 

     for rows.Next() { 
      b := Gallery{} 
      err := rows.Scan(&b.Uri, &b.Title, &b.Content) 
      // Handle error here 

      gallery = append(gallery, b) 
     } 

     return c.Render(http.StatusOK, "onlytestingtpl", gallery[0]) 
    }) 

    e.Run(":4444") 
} 

回答

1

這是由HTML模板引起的 - 你可以使用的功能,如:

func unsafe(x string) template.HTML { 
    return template.HTML(x) 
} 

獲得 '轉義' 的HTML。

+0

它來自'unsafe'包嗎?因爲我使用Echo框架,我應該在哪裏放置代碼? –

+0

它來自html/template包 - 我不熟悉Echo框架。我剛剛命名了我在「不安全」答案中寫的函數。你應該可以把任何地方放到真正的地方,並將你的內容字符串傳遞給它。 – rebelbass

+0

非常感謝。我仍在努力尋找解決方案。我正在使用'text/template'作爲臨時解決方法。 –