Skip to content

Learn Go template

Go template library is a powerful library. Some examples can be found below. repeatit.io is a great place to learn/practice templating. Following examples will redirect you to the repeatit.io.

Use a value as text

If you have a value, for example Port, and want to write its value as text, you should write it between delimiters {{ }} with a leading . as {{.Port}} See the examples:

Port Example

Mail Example

If, else if, else

Go template library allows you to do conditional rendering with if statements . You can use following keywords in if statements to compare values

eq
	Returns the boolean truth of arg1 == arg2
ne
	Returns the boolean truth of arg1 != arg2
lt
	Returns the boolean truth of arg1 < arg2
le
	Returns the boolean truth of arg1 <= arg2
gt
	Returns the boolean truth of arg1 > arg2
ge
	Returns the boolean truth of arg1 >= arg2

See examples:

HTTP server example

To check boolean

Iterate an array and map

Go template allows you to iterate an array or a map.

See examples:

Iterate an array

Iterate a map