GoLand 2026.3 Help

Tutorial: Create a live template with variables and functions

Create a live template 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:

Create a live template with variables

  1. Press Ctrl+Alt+S to open settings and then select Editor | Live Templates.

  2. Select the Go group, click the Add button, 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:

    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
  7. Click OK to close the variable editor, then click OK in the settings dialog to save the template.

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 to jump to the next variable.

  4. Using the keyboard arrows, select grass as a value of the food string and press Enter.

22 September 2026