- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
根据 documentation Go 标准库中的 text/template
包,(据我所知,html/template
在这里是一样的)只需使用管道运算符就会吐出一个无论是什么的“默认文本表示”:
{{pipeline}}
The default textual representation of the value of the pipeline is copied to the output.
在 map 的情况下,您会得到一个很好的打印格式,其中包含键名和所有内容……顺便说一下,这是有效的 JavaScript,因此如果您愿意,可以轻松地将整个结构传递到您的 JS 代码中。
我的问题是,这个文本表示是如何确定的,更具体地说,我可以 Hook 它吗?我想也许它会检查管道是否是 fmt.Stringer
并且我可以给我的 map 子类型一个 String() string
方法,但这似乎不是是这样的。我正在寻找 text/template
代码,但我似乎不知道它是如何做到这一点的。
text/template
如何确定“默认文本表示”?
最佳答案
默认文本表示由 fmt
的方式决定包打印值。所以你找对了树。
看这个例子:
t := template.Must(template.New("").Parse("{{.}}"))
m := map[string]interface{}{"a": "abc", "b": 2}
t.Execute(os.Stdout, m)
输出:
map[a:abc b:2]
现在,如果我们使用带有 String()
方法的自定义 map 类型:
type MyMap map[string]interface{}
func (m MyMap) String() string { return "custom" }
mm := MyMap{"a": "abc", "b": 2}
t.Execute(os.Stdout, mm)
输出是:
custom
在 Go Playground 上尝试这些(以及下面的示例) .
请注意 MyMap.String()
有一个值接收器(不是指针)。我传递了 MyMap
的值,所以它起作用了。如果您将接收器类型更改为指向 MyMap
的指针,它将不起作用。这是因为只有 *MyMap
类型的值才有 String()
方法,而 MyMap
的值没有。
如果 String()
方法有一个指针接收者,你必须传递 &mm
(*MyMap
类型的值)如果你希望您的自定义表示起作用。
另请注意,在 html/template
的情况下,模板引擎会进行上下文转义,因此 fmt
包的结果可能会进一步转义。
例如,如果您的自定义 String()
方法会返回“不安全”的内容:
func (m MyMap2) String() string { return "<html>" }
尝试插入:
mm2 := MyMap2{"a": "abc", "b": 2}
t.Execute(os.Stdout, mm2)
被转义:
<html>
这是在 text/template
中实现的地方包裹:text/template/exec.go
,未导出函数 state.PrintValue()
,当前行 #848:
_, err := fmt.Fprint(s.wr, iface)
如果您使用的是 html/template
包,它在 html/template/content.go
中实现,未导出函数 stringify()
,当前行 #135:
return fmt.Sprint(args...), contentTypePlain
另请注意,如果该值实现了 error
, Error()
方法将被调用并且它优先于 String()
:
type MyMap map[string]interface{}
func (m MyMap) Error() string { return "custom-error" }
func (m MyMap) String() string { return "custom" }
t := template.Must(template.New("").Parse("{{.}}"))
mm := MyMap{"a": "abc", "b": 2}
t.Execute(os.Stdout, mm)
将输出:
custom-error
而不是自定义
。在 Go Playground 上试用.
关于string - text/template如何确定 map 的 "default textual representation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38518866/
template struct List { }; template class> struct ListHelper; template struct ListHelper> { };
最近,我注意到 html/template.Template 的 Templates() 与 text/template.Template 的工作方式不同。 // go1.12 func main()
我正在尝试使用 polymer 1.0 实现一个网站。我有一个自定义元素 my-greeting,里面有一些模板重复。 我想做的是获取一个名为 TARGET 的字符串,但我不知道该怎么做: /cons
(是的,由于我糟糕的英语,标题很奇怪;我希望有人能改进它。) 接听this question ,我发现这段代码有效: template class A { }; template class U>
这个问题在这里已经有了答案: How to import and use different packages of the same name (2 个答案) 关闭 4 年前。 我正在使用 Go
我的想法是这是不可能的,或者我缺少一个额外的步骤。无论哪种方式,我都被卡住了,无法弄清楚。 使用内联模板的原因是能够使用 Laravel Blade 语法并结合 Vue Js 的强大功能。似乎是两者中
我已经尝试实现一个“模板模板模板”——模板类来满足我的需求(我对使用模板元编程很陌生)。不幸的是,我发现以下主题为时已晚: Template Template Parameters 不过,我需要实现如
Helm _helpers.tpl? Helm 允许使用 Go templating在 Kubernetes 的资源文件中。 一个名为 _helpers.tpl 的文件通常用于定义 Go 模板助手,语
{{template "base"}} 和 {{template "base".}} 有什么区别? 我用的是go-gin,两者都可以正常运行。我在文档中找不到关于此的任何描述。 最佳答案 来自 god
我有一个本质上充当查找表的函数: function lookup(a::Int64, x::Float64, y::Float64) if a == 1 z = 2*x + y else if a =
当 out 成员函数(来自模板和特化)都需要模板时,为什么 c++ 需要模板参数,因为我没有得到它,谷歌也没有帮助。必须是c++11但和c++1z有同样的错误。 我正在使用 g++ 7.3.0 收到此
我正在寻找简单的方法来将带有 ${myvar} 的简单模板转换为带有 {{ myvar }} 的 GO 模板。 是否有任何库可以实现这一点? 最佳答案 使用正则表达式查找 \${([a-z0-9\_\
我有这个模板可以将 slice 的多个项目解析到页面上。它确实做得很好。 但是,我现在想使用完全相同的模板来根据范围索引解析 slice 的单个值。该 slice 在多个文件中使用,所以我不能像 Sl
要清理模板文件夹,我想将常用模板保存在子文件夹中。目前我有以下文件结构: main.go templates/index.tpl # Main template for the
最近我设计了元类型和允许编译时类型连接的可能操作: #include template typename T> struct MetaTypeTag {}; /*variable template
准备模板时发生错误。谁能告诉你怎么修? 如有必要,还可以编辑变量。 vars: AllСountry: - "name1" - "name2"
我在使用新的匿名模板引擎时遇到问题。它不能使用嵌套模板。我收到错误消息:“此模板引擎不支持嵌套在其模板中的匿名模板”。 我的问题:我如何强制 knockout JS 使用jquery 模板引擎,而不是
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 8
我在 C++ 中使用带有模板的集合: template class OMSSVDisk : public OMSSObjProperties{ set memberPDs; }; 如上面代码中
因为我喜欢分离接口(interface)和实现,而不是只在头文件中实现模板类,我将它分成 .h 和 .tpp(.tpp 这样它就不会用 *.cpp 编译)。然后我将 tpp 包含在头文件的末尾,就在
我是一名优秀的程序员,十分优秀!