如何导入HTML类型?

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

How to import the HTML type?

问题

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

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

我该如何导入和使用它?

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

package main

import (
	"fmt"
	"html/template"
)

func main() {
	fmt.Println(HTML(`<strong>Hi</strong>`))
}
英文:

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):

package main

import (
	&quot;fmt&quot;
	&quot;html/template&quot;
)

func main() {
	fmt.Println(HTML(`&lt;strong&gt;Hi&lt;/strong&gt;`))
}

答案1

得分: 1

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

package main

import (
	"fmt"
	"html/template"
)

func main() {
	fmt.Println(template.HTML(`<strong>Hi</strong>`))
}

playground 示例

英文:

Scope the types with the package name:

package main

import (
  &quot;fmt&quot;
  &quot;html/template&quot;
)

func main() {
   fmt.Println(template.HTML(`&lt;strong&gt;Hi&lt;/strong&gt;`))
}

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:

确定