TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Using Go Templates

120 点作者 bketelsen超过 7 年前

8 条评论

nishs超过 7 年前
An additional tip. Template actions don&#x27;t necessarily have to be delimited by &#x27;{{&#x27; and &#x27;}}&#x27;.<p>Before parsing, you can call Delims to use custom delimiters.<p><pre><code> func (t *Template) Delims(left, right string) *Template </code></pre> <a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;text&#x2F;template&#x2F;#Template.Delims" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;text&#x2F;template&#x2F;#Template.Delims</a>
评论 #16019820 未加载
评论 #16019496 未加载
abc-xyz超过 7 年前
I really want to love templates, but I find many aspects to be quite confusing.<p>(1): I just want to deploy a binary file. This does not seem possible with templates (unless they&#x27;re defined inside .go files, but never seen anyone do that).. also rather annoying that the tmpl files must be located in a specific directory rather than located among their components.. but I guess it wouldn&#x27;t make much sense anyway since they won&#x27;t be a part of the binary file.<p>(2): it&#x27;s really difficult to find good and medium-large scale projects that are using templates<p>(3): I have no idea what&#x27;s the right way to initialize and execute templates, this includes when the same template will be called in multiple funcs. I know how I can do it, but I don&#x27;t feel confident that I&#x27;m doing it correctly..<p>(4): it&#x27;s really exhausting to keep template code clean. With Go (and various JS frameworks) the code is automatically formatted&#x2F;linted&#x2F;whatever, but with templates nada.. can&#x27;t even figure out how to minimize it for production.<p>(5): no idea what to do with css&#x2F;js.. sometimes use inline, other times call a static file. Static files obviously benefit from being cached.. not sure how I&#x27;m supposed to cache the templates<p>I can&#x27;t really tell if there are any speed gains to using templates, so it feels like a lot of sacrifices for no obvious gain. I do think templates would be godlike if they featured vue-like structure with &quot;template&#x2F;script&#x2F;style&quot; syntax, and was easily minified and compiled into the binary. But I guess I can keep dreaming about the last part :)
评论 #16022983 未加载
评论 #16020424 未加载
评论 #16020598 未加载
评论 #16020878 未加载
评论 #16021428 未加载
评论 #16020983 未加载
评论 #16021219 未加载
enahs-sf超过 7 年前
Really glad someone put this together. The official documentation is pretty good but somewhat lacking in terms of practical examples around templates.
tschellenbach超过 7 年前
This Django style template library is also quite slick: <a href="https:&#x2F;&#x2F;github.com&#x2F;flosch&#x2F;pongo2" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;flosch&#x2F;pongo2</a>
piotrkubisa超过 7 年前
I am also really glad someone has glued toghether major aspects of template&#x2F;html std library. It is definetely a place where are some &#x27;taboos&#x27;, tricks or gotchas, i.e. re-usability of tmpl static variable to add multiple named templates and call them by name (re: question on disqus below article) or sharing parent scope to children variable when using range loop (stackoverflow popular question). I am bit sad I haven&#x27;t found some free time to collect all known &#x27;tricks&#x27; to me and try to list them somehow or send CR yet.
评论 #16019190 未加载
dzink超过 7 年前
Have you tried to benchmark writing out a page with Go templates, vs GO producing a JSON API that prints out into static HTML pages with JQUERY&#x2F;Handlebars? How many RPS can you get with either scenario? What is Time to First Paint and when does the page finish printing for the user?
评论 #16020182 未加载
gkya超过 7 年前
Seeing this from the front page RSS feed I opened the comments page in shock thinking somehow Golang got templates a la C++...
tonetheman超过 7 年前
Gopher Academy has been doing great content lately. Good examples for templates past the real docs. Good stuff.