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" }}
  • ne checks if the two values are not equal.
    • eg: {{ if ne .Name "User" }}
  • lt checks if the first value is less than the second value.
    • eg: {{ if lt .Age 18 }}
  • le checks if the first value is less than or equal to the second value.
    • eg: {{ if le .Age 18 }}
  • gt checks if the first value is greater than the second value.
    • eg: {{ if gt .Age 18 }}
  • ge checks if the first value is greater than or equal to the second value.
    • eg: {{ if ge .Age 18 }}
  • and checks if both the values are true.
    • eg: {{ if and (eq .Name "User") (ge .Age 18) }}
  • or checks if either of the values are true.
    • eg: {{ if or (eq .Name "User") (ge .Age 18) }}
  • not checks if the value is false.
    • eg: {{ if not (eq .Name "User") }}
  • in checks if the value is in the list.
    • eg: {{ if in .Name "User" "Admin" }}

arithmetic operators

  • add adds the two values.
    • eg: {{ add .Age 2 }}
  • sub subtracts the second value from the first value.
    • eg: {{ sub .Age 2 }}
  • mul multiplies the two values.
    • eg: {{ mul .Age 2 }}
  • div divides the first value by the second value.
    • eg: {{ div .Age 2 }}
  • mod checks if the first value is divisible by the second value.
    • eg: {{ if mod .Age 2 }}

string operators

  • title converts the first character of the string to uppercase.
    • eg: {{ title .Name }}
  • lower converts the string to lowercase.
    • eg: {{ lower .Name }}
  • upper converts the string to uppercase.
    • eg: {{ upper .Name }}
  • trim removes the leading and trailing whitespace from the string.
    • eg: {{ trim .Name }}
  • trimAll removes all the leading and trailing whitespace from the string.
    • eg: {{ trimAll .Name }}
  • trimPrefix removes the leading prefix from the string.
    • eg: {{ trimPrefix .Name "Mr." }}
  • trimSuffix removes the trailing suffix from the string.
    • eg: {{ trimSuffix .Name "Jr." }}
  • replace replaces the old string with the new string.
    • eg: {{ replace .Name "Mr." "Dr." }}
  • repeat repeats the string n times.
    • eg: {{ repeat .Name 2 }}
  • split splits the string into a list of strings.
    • eg: {{ split .Name " " }}
  • join joins the list of strings into a single string.
    • eg: {{ join .Name " " }}
  • contains checks if the string contains the substring.
    • eg: {{ if contains .Name "User" }}
  • hasPrefix checks if the string has the prefix.
    • eg: {{ if hasPrefix .Name "Mr." }}
  • hasSuffix checks if the string has the suffix.
    • eg: {{ if hasSuffix .Name "Jr." }}

list operators

  • range iterates over the list.
    • eg: {{ range .Name }} {{ . }} {{ end }}
  • len returns the length of the list.
    • eg: {{ len .Name }}
  • sort sorts the list.
    • eg: {{ sort .Name }}
  • first returns the first value of the list.
    • eg: {{ first .Name }}
  • last returns the last value of the list.
    • eg: {{ last .Name }}
  • after returns the list after the given index.
    • eg: {{ after .Name 0 }}
  • before returns the list before the given index.
    • eg: {{ before .Name 0 }}
  • in checks if the value is in the list.
    • eg: {{ if in .Name "User" "Admin" }}