How to calculate something in html/template
问题 How can you calculate something inside a html template of go? For example: {{ $length := len . }} <p>The last index of this map is: {{ $length -1 }} </p> Were the . is a map. The code {{ $length -1 }} is not working, is there a way to achieve this? 回答1: You can't. Templates are not a scripting language. By design philosophy, complex logic should be outside of templates. Either pass the calculated result as a parameter (preferred / easiest), or register custom functions which you can call