Formatting
You can supply formatting rules when interpolating Go and in evaluated Go output to use non-string types. In Haml and Slim this applies to #{...} interpolation and = output. In EGO this applies to <%= ... %> and <%! ... %> output tags. Normally, you would only be able to output string values. But if you use any of the valid formatting codes, such as %d or %02f followed by a space and then the value you wish to output, GoHT will know that you are trying to output a non-string value and will parse it accordingly.
Formatting Go Values
If you provide a formatting rule before a variable, for example, like #{rule variable}, the variable will be formatted according to the rule.
renders as:
You can also provide a formatting rule for evaluated Go code.
renders as:
Common Formatting Rules
The Printing section of the Go docs has a list of all the formatting rules you can use. Here are a few common ones:
True/False
We can get either true or false from a boolean value by using %t.
Often you will want to have a string representation of a boolean value in your HTML.
Instead of adding in some Go code such as this:
You can use the %t formatting rule to output the boolean value as a string.
renders as:
If Helper
GoHT provides a helper function to output a boolean value as a string other than “true” or “false”.
renders as:
Decimal Places
You can use %f to output a floating-point number to a certain number of decimal places.
renders as:
Padding
You can use %02d to pad an integer with leading zeros.
renders as:
You can also pad a string with spaces.
renders as:
Date and Time
You can use %v to quickly output a date or time.
renders as:
Conclusion
Formatting Go values is a powerful feature of GoHT. It allows you to output non-string values in your HTML without having to write a lot of Go code. It also allows you to apply some simple formatting at the same time.