go - 如何导入HTML类型?

标签 go

html/template documentation提到一个 HTML 类型:

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

如何导入和使用它?

我试过下面的代码,它抛出一个“undefined: HTML”错误(Go Playground):

package main

import (
    "fmt"
    "html/template"
)

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

最佳答案

用包名定义类型:

主要包

import (
  "fmt"
  "html/template"
)

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

playground example

关于go - 如何导入HTML类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28058496/

相关文章:

windows - 问golang中的LookupTXT函数

go - splitsh/lite:在Windows 10上构建时出错

json - 如何在 Go 的 map[string]interface{} 中做 deep sets 和 gets?

go - 如何使用 Go 在电子邮件中包含 CC 和 BCC

go - 如何在golang中制作绝对路径http重定向

go - 如何等待多个 goroutine 完成?

go - 如何优雅地使 slice 追加安全

go - 解析数字字符串数字

templates - Golang 新模板不起作用

mongodb - 计算数组中元素的数量