Go模板示例

35

我想知道有哪些关于使用Go html/templates的好的、最新的资源,特别是在解析html文件和处理列表方面。我发现 Hoka Poka 网站,但它似乎已过时。


2
你是指 text/template 还是 html/template - Matt K
对于其中任何一个,源代码分发中的单元测试都可能是很好的示例。 - Matt K
4个回答

41

2
最后一个已更新以反映模板包的最新更改,并解释了模板集。 - JohnDoe
@JohnDoe,模板集在最新版中已经过时。 - Mike Samuel
@MikeSamuel: 最新的每周版或开发版本?你能给我一个关键词来浏览帮助文档吗?因为集合功能(例如 .Add)仍然存在。 - JohnDoe
截至2011年11月23日的主干版本中,Set类型已不再存在。 - Mike Samuel

5
Google App Engine文档提供了一个简单的示例,展示如何使用templaterange来渲染列表。请查看Google App Engine文档

5

"html/template"相关的模板代码片段可以在文档的这里找到。

Contexts

Assuming {{.}} is O'Reilly: How are <i>you</i>?, the table below shows how {{.}} appears when used in the context to the left.

Context                          {{.}} After
{{.}}                            O'Reilly: How are &lt;i&gt;you&lt;/i&gt;?
<a title='{{.}}'>                O&#39;Reilly: How are you?
<a href="/{{.}}">                O&#39;Reilly: How are %3ci%3eyou%3c/i%3e?

测试套件有许多示例:https://code.google.com/p/go/source/browse/src/html/template/escape_test.go

如果想要查看模板的实际应用,请查看godoc源代码。从http://code.google.com/p/go/source/browse/src/cmd/godoc/godoc.go#642开始是一个很好的起点。


1

这些示例是针对 template 而不是 html/template - Mike Samuel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接