<p>Lets say that I have some text coming in dynamically which I have to parse with templates. Now the text might or might not contain any template Action.</p>
<p>For eg, it might be </p>
<p>"[app_name] {{.Timestamp}}- " or</p>
<p>"[app_name] no template- " or</p>
<p>Of course, doing a template execute on either of these has the same result. But my task is to just prepend the resulting text to another string, so I would rather avoid template parsing and just concatenate the string to my target string, if there is no template action to parse. Hence, my question of how to detect whether the text contains an Action or not ?</p>
<p>Should I just do a regex match and be done with it ? Or there is a more idiomatic way to achieve this ?</p>
<p>Thanks !</p>
<hr/>**评论:**<br/><br/>captncraig: <pre><p>One option would be to use the underlying parse package directly. </p>
<p><a href="https://golang.org/pkg/text/template/parse/" rel="nofollow">https://golang.org/pkg/text/template/parse/</a></p>
<p><code>Parse</code> will give you back a set of Trees (usually only one if you call it right). That tree will have a single list node. If there are no actions, that list should have exactly one member, which will be a TextNode. </p>
<p>Its an option. Are you reusing templates multiple times, or is it an on-demand type thing?. Have you benchmarked just doing the full template every time? What are your performance requirements? Regex is not exactly fast either, and I imagine executing a single text node would be pretty fast, with parsing being the biggest time sink, but also probably pretty quick compared to network for example.</p></pre>qu33ksilver: <pre><blockquote>
<p>Parse will give you back a set of Trees (usually only one if you call it right). That tree will have a single list node. If there are no actions, that list should have exactly one member, which will be a TextNode.</p>
</blockquote>
<p>Thanks. Very clean and elegant !</p>
<pre><code>t := template.Must(template.New("line").Parse(templateStr))
fmt.Println(len(t.Tree.Root.Nodes))
</code></pre>
<p>I don't want to do template execute every time unnecessarily because this is in the hot path of my application. No, I haven't benchmarked it. This is a personal project of mine that I have just started. So focusing on creating the basic features now. But definitely will benchmark once I reach a stable version.</p>
<p>Thanks for the help ! </p></pre>captncraig: <pre><p>Looking at the source for the execution: <a href="https://golang.org/src/text/template/exec.go?s=5006:5070#L220" rel="nofollow">https://golang.org/src/text/template/exec.go?s=5006:5070#L220</a> if the template is just a text node, there really isn't anything more it does than copy the text to the output stream. I'd be surprised if it was worth your effort to optimize that.</p></pre>qu33ksilver: <pre><p>Its just not that. The values of the template are going to be dynamically populated. So if there is no template action, those values will be redundantly created. </p>
<p>edit: and the execute code does a state.walk which has considerable code.</p></pre>captncraig: <pre><p>Make a lazy-loading context type. And the walk code literally takes the simplest possible path if it is a simple text node. Without benchmarks there is absolutely no reason to think you need to optimize that.</p></pre>captncraig: <pre><p>Ran a few benchmarks: <a href="https://gist.github.com/captncraig/472477bcfbdb7b2f628395a5d84555d5" rel="nofollow">https://gist.github.com/captncraig/472477bcfbdb7b2f628395a5d84555d5</a> </p>
<p>Executing the template comes in under 200ns, which is literally nothing. Just dumping the bytes does cost less, but at a cost of having to care about the content of the template you are dealing with. </p></pre>qu33ksilver: <pre><p>wow, thanks for running the benchmarks ! I will keep this is consideration in future.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传