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.
conditional operators arithmetic operators string operators list operatorsAll operators reference
eq
checks if the two values are equal.
{{ if eq .Name "User" }}
ne
checks if the two values are not equal.
{{ if ne .Name "User" }}
lt
checks if the first value is less than the second value.
{{ if lt .Age 18 }}
le
checks if the first value is less than or equal to the second value.
{{ if le .Age 18 }}
gt
checks if the first value is greater than the second value.
{{ if gt .Age 18 }}
ge
checks if the first value is greater than or equal to the second value.
{{ if ge .Age 18 }}
and
checks if both the values are true.
{{ if and (eq .Name "User") (ge .Age 18) }}
or
checks if either of the values are true.
{{ if or (eq .Name "User") (ge .Age 18) }}
not
checks if the value is false.
{{ if not (eq .Name "User") }}
in
checks if the value is in the list.
{{ if in .Name "User" "Admin" }}
add
adds the two values.
{{ add .Age 2 }}
sub
subtracts the second value from the first value.
{{ sub .Age 2 }}
mul
multiplies the two values.
{{ mul .Age 2 }}
div
divides the first value by the second value.
{{ div .Age 2 }}
mod
checks if the first value is divisible by the second value.
{{ if mod .Age 2 }}
title
converts the first character of the string to uppercase.
{{ title .Name }}
lower
converts the string to lowercase.
{{ lower .Name }}
upper
converts the string to uppercase.
{{ upper .Name }}
trim
removes the leading and trailing whitespace from the string.
{{ trim .Name }}
trimAll
removes all the leading and trailing whitespace from the string.
{{ trimAll .Name }}
trimPrefix
removes the leading prefix from the string.
{{ trimPrefix .Name "Mr." }}
trimSuffix
removes the trailing suffix from the string.
{{ trimSuffix .Name "Jr." }}
replace
replaces the old string with the new string.
{{ replace .Name "Mr." "Dr." }}
repeat
repeats the string n times.
{{ repeat .Name 2 }}
split
splits the string into a list of strings.
{{ split .Name " " }}
join
joins the list of strings into a single string.
{{ join .Name " " }}
contains
checks if the string contains the substring.
{{ if contains .Name "User" }}
hasPrefix
checks if the string has the prefix.
{{ if hasPrefix .Name "Mr." }}
hasSuffix
checks if the string has the suffix.
{{ if hasSuffix .Name "Jr." }}
range
iterates over the list.
{{ range .Name }} {{ . }} {{ end }}
len
returns the length of the list.
{{ len .Name }}
sort
sorts the list.
{{ sort .Name }}
first
returns the first value of the list.
{{ first .Name }}
last
returns the last value of the list.
{{ last .Name }}
after
returns the list after the given index.
{{ after .Name 0 }}
before
returns the list before the given index.
{{ before .Name 0 }}
in
checks if the value is in the list.
{{ if in .Name "User" "Admin" }}