# Tutorial: Create a live template with variables and functions

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/go/2026.3/llms.txt).

Create a [live template](using-live-templates.html) that generates a Go struct and a method. Template variables let you enter a type name and select a food value when you insert the template.

* Create a new struct type with a `food` field of type `string`.

* Implement a `PetFood()` method that stores the selected food in a local variable and prints a message. The method does not read or change the struct field.

To showcase how variables and functions work in templates, we will add the following variables to the template text:

Procedure: Create a live template with variables

1. Press `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) to open settings and then select `Editor | Live Templates`.

2. Select the     Go  group, click ![the Add button](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg), and select Live Template.

3. In the Abbreviation field, specify the characters that will be used to expand the template. For example, `pet`.

4. In the Template text field, paste the following template:

```GO
type $TypeName$ struct {
    food string
}

func (p *$TypeName$) PetFood() {
    food := "$Food$"
    println("The $typeName$ eats " + food)
}
```

5. Below the template text, click Define and select `Go | File`. This makes the template available at the top level of a Go file, outside functions and other declarations.

6. Click Edit Variables… and, in the Edit Template Variables dialog, configure the variables:

* `$TypeName$`: leave the Expression field empty. When using the template, GoLand will prompt the user to enter a type name after inserting the template.

* `$typeName$`: in the Expression field, enter `decapitalize (TypeName)`. This converts the first letter of `$TypeName$` to lower case. Select Skip if defined.

* `$Food$`: in the Expression field, enter `enum("meat","grass", "fruit")`. When using the template, GoLand will display a list of these values in the editor to choose from.

![Live template context](https://resources.jetbrains.com/help/img/idea/2026.3/go_live_template_example_variables.png)

7. Click OK to close the variable editor, then click OK in the settings dialog to save the template.

Procedure: Use the created template

1. Open a Go file and place the caret on a blank line after the `package` and any `import` declarations, outside a function or type declaration.

2. In the editor, start typing the template abbreviation (`pet` in our example) and select it from the completion dropdown.

3. Enter the type name as a variable value: `Horse`. Press  `Tab` (Windows), `⇥` (macOS), `⇥` (IntelliJ IDEA Classic (macOS)), `⇥` (macOS System Shortcuts), `Tab` (XWin), `Tab` (GNOME), `Tab` (KDE), `Tab` (Emacs), `Tab` (Sublime Text), `⇥` (Sublime Text (macOS)), `Tab` (VSCode), `⇥` (VSCode (macOS)) to jump to the next variable.

4. Using the keyboard arrows, select `grass` as a value of the `food` string  and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (VSCode), `⏎` (VSCode (macOS)).

[Video](https://resources.jetbrains.com/help/img/idea/2026.3/go_live_template_example_usage.mp4)

