Let’s check the Go template syntax and essentials.
Placeholders
The {{ }}
is used to denote a placeholder. It can contain a variable, function, control statments like if, for, range etc.
a variant of placeholder is {{- -}}
which is used to trim the whitespace before the placeholder, {{-
trims the whitespace before the placeholder and -}}
trims the whitespace after the placeholder.
Variables
Variables are defined using the {{ $variableName := value }}
syntax. The variable can be used in the template using {{ $variableName }}
.
example:
{{ $name := "User" }}
<h1>Hello {{ $name }}</h1>
Functions
Functions are defined using the {{ funcName arg1 arg2 }}
syntax. The function can be used in the template using {{ funcName arg1 arg2 }}
.
example:
{{ $name := "User" }}
<h1>Hello {{ upper $name }}</h1>
Note: upper is a function that converts the string to uppercase.
func upper(s string) string { return strings.ToUpper(s) }
Control Statements
if else
Control statements are defined using the {{ if condition }} {{ else if condition }} {{ else }} {{ end }}
syntax.
example:
{{ if eq .Name "User" }}
<h1>Hello User</h1>
{{ else }}
<h1>Hello Stranger</h1>
{{ end }}
Note: eq is a function that checks if the two values are equal.
range
range is defined using the {{ range $val := . }} {{ end }}
syntax. where . is the variable passed to the template, which is a list of values. []int{1, 2, 3}
.
example:
{{ range $val := . }}
<h1>{{ $val }}</h1>
{{ end }}
returns
<h1>1</h1>
<h1>2</h1>
<h1>3</h1>
Note: .UserList is a variable that contains a list of users.
Operators
Operators are used to perform operations on the values. They return a value that can be used in the template.
All operators reference
conditional operators
eq
checks if the two values are equal.- eg:
{{ if eq .Name "User" }}
- eg:
ne
checks if the two values are not equal.- eg:
{{ if ne .Name "User" }}
- eg:
lt
checks if the first value is less than the second value.- eg:
{{ if lt .Age 18 }}
- eg:
le
checks if the first value is less than or equal to the second value.- eg:
{{ if le .Age 18 }}
- eg:
gt
checks if the first value is greater than the second value.- eg:
{{ if gt .Age 18 }}
- eg:
ge
checks if the first value is greater than or equal to the second value.- eg:
{{ if ge .Age 18 }}
- eg:
and
checks if both the values are true.- eg:
{{ if and (eq .Name "User") (ge .Age 18) }}
- eg:
or
checks if either of the values are true.- eg:
{{ if or (eq .Name "User") (ge .Age 18) }}
- eg:
not
checks if the value is false.- eg:
{{ if not (eq .Name "User") }}
- eg:
in
checks if the value is in the list.- eg:
{{ if in .Name "User" "Admin" }}
- eg:
arithmetic operators
add
adds the two values.- eg:
{{ add .Age 2 }}
- eg:
sub
subtracts the second value from the first value.- eg:
{{ sub .Age 2 }}
- eg:
mul
multiplies the two values.- eg:
{{ mul .Age 2 }}
- eg:
div
divides the first value by the second value.- eg:
{{ div .Age 2 }}
- eg:
mod
checks if the first value is divisible by the second value.- eg:
{{ if mod .Age 2 }}
- eg:
string operators
title
converts the first character of the string to uppercase.- eg:
{{ title .Name }}
- eg:
lower
converts the string to lowercase.- eg:
{{ lower .Name }}
- eg:
upper
converts the string to uppercase.- eg:
{{ upper .Name }}
- eg:
trim
removes the leading and trailing whitespace from the string.- eg:
{{ trim .Name }}
- eg:
trimAll
removes all the leading and trailing whitespace from the string.- eg:
{{ trimAll .Name }}
- eg:
trimPrefix
removes the leading prefix from the string.- eg:
{{ trimPrefix .Name "Mr." }}
- eg:
trimSuffix
removes the trailing suffix from the string.- eg:
{{ trimSuffix .Name "Jr." }}
- eg:
replace
replaces the old string with the new string.- eg:
{{ replace .Name "Mr." "Dr." }}
- eg:
repeat
repeats the string n times.- eg:
{{ repeat .Name 2 }}
- eg:
split
splits the string into a list of strings.- eg:
{{ split .Name " " }}
- eg:
join
joins the list of strings into a single string.- eg:
{{ join .Name " " }}
- eg:
contains
checks if the string contains the substring.- eg:
{{ if contains .Name "User" }}
- eg:
hasPrefix
checks if the string has the prefix.- eg:
{{ if hasPrefix .Name "Mr." }}
- eg:
hasSuffix
checks if the string has the suffix.- eg:
{{ if hasSuffix .Name "Jr." }}
- eg:
list operators
range
iterates over the list.- eg:
{{ range .Name }} {{ . }} {{ end }}
- eg:
len
returns the length of the list.- eg:
{{ len .Name }}
- eg:
sort
sorts the list.- eg:
{{ sort .Name }}
- eg:
first
returns the first value of the list.- eg:
{{ first .Name }}
- eg:
last
returns the last value of the list.- eg:
{{ last .Name }}
- eg:
after
returns the list after the given index.- eg:
{{ after .Name 0 }}
- eg:
before
returns the list before the given index.- eg:
{{ before .Name 0 }}
- eg:
in
checks if the value is in the list.- eg:
{{ if in .Name "User" "Admin" }}
- eg: