Using live templates
By using Live templates, you can insert frequently-used constructions into your code. For example, loops, conditions, various declarations,or print statements. To expand a code snippet, type the corresponding template abbreviation and press Tab. Keep pressing Tab to jump from one variable in the template to the next one. Press Shift+Tab to move to the previous variable. For more information, see Live Template Variables.
Insert a live template
Place the caret at the place where you want the template to expand.
Type the template abbreviation and press the invocation key (usually Tab by default). Alternatively, on the suggestion list and select the necessary template.
menu, click (Ctrl+J) to open theIf the selected template requires user input, the corresponding field is highlighted. Type the necessary value and press Enter or Tab to complete input and move to the next input field. After completing all input fields, the caret moves to the end of the construct (or to the
$END$
marker if the marker is defined in the template code), and the editor returns to the regular mode of operation.
Surround a block of code with a live template
Select a piece of code to be surrounded.
On the suggestion list and select the necessary template.
menu, click (Ctrl+Alt+J) to open the
Consider the following example of the Hello World program that is coded with Live Templates.
The following types of live templates are distinguished:
-
Simple templates contain only fixed plain text. When you expand a simple template, the text is automatically inserted into your source code, replacing the abbreviation.
Abbreviation
Expands to
imp
import ( )
int
func init() { }
main
func main() { }
-
Parameterized templates contain variables that enable user input. When you expand a parameterized template, variables are either replaced by input fields for the user to specify manually, or calculated by GoLand automatically.
Abbreviation
Expands to
forr
for key, value := range collection { }
meth
func (receiver ) name(params) { }
meth
func (receiver ) name(params) { }
-
Surround templates wrap a block of the selected code with the text specified by the user.
To configure live templates, open the Live Templates page, you can see all the available live templates, edit them and create new templates. Templates are grouped according to the context where they can be used (usually, by the corresponding language). To move a template to another group, right-click the template, select Move, and then select the necessary group name.
page of GoLand settings (Ctrl+Alt+S). On theEach live template is defined by an abbreviation containing alphanumeric characters, dots, and hyphens. The abbreviation must be unique within a group, but the same abbreviation may be used in different groups and expand to different constructs according to the context of the corresponding group. Abbreviations of modified default templates are shown in the list with blue font.
Restore a modified template to the default settings
On the Live Templates page of the Settings / Preferences Dialog, right-click the template which you want to restore and click Restore defaults.