如何导入HTML类型?

huangapple go评论113阅读模式
英文:

How to import the HTML type?

问题

html/template文档中提到了一个HTML类型:

> HTML、JS、URL和其他来自content.go的类型可以携带安全内容,免于转义。

我该如何导入和使用它?

我尝试了以下代码,但会抛出一个"undefined: HTML"错误(Go Playground):

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. )
  6. func main() {
  7. fmt.Println(HTML(`<strong>Hi</strong>`))
  8. }
英文:

The html/template documentation mentions an HTML type:

> Types HTML, JS, URL, and others from content.go can carry safe content
> that is exempted from escaping.

How can I import and use it?

I have tried the following code, which throws an "undefined: HTML" error (Go Playground):

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;html/template&quot;
  5. )
  6. func main() {
  7. fmt.Println(HTML(`&lt;strong&gt;Hi&lt;/strong&gt;`))
  8. }

答案1

得分: 1

将类型与包名进行作用域限定:

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. )
  6. func main() {
  7. fmt.Println(template.HTML(`<strong>Hi</strong>`))
  8. }

playground 示例

英文:

Scope the types with the package name:

package main

  1. import (
  2. &quot;fmt&quot;
  3. &quot;html/template&quot;
  4. )
  5. func main() {
  6. fmt.Println(template.HTML(`&lt;strong&gt;Hi&lt;/strong&gt;`))
  7. }

playground example

huangapple
  • 本文由 发表于 2015年1月21日 10:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/28058496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定