Effective Coding with Custom Live Templates in GoLand
Live Templates a.k.a. code snippets can be a very powerful tool in your day-to-day work. Who wouldn’t like to write whole blocks of code by pressing just a few keystrokes? Eliminate some routine, and you’ll have more time to focus on what’s really important.
Imagine you are working on web applications. How many times do you write such code patterns as a setting response header to UTF-8, TLS configurations, connecting to databases, and so on? This is exactly the area where code snippets save our time and effort.
But when that’s not enough, you’ll want to create your own templates. Let’s create several custom ones together. Before we start, let’s take a quick look at how it works inside.
A Live Template can be one of three types:
Simple templates contain only fixed plain text and just replace the abbreviation.
Parameterized templates contain plain text with variables that enable user input.
Surround templates wrap a block of selected code with the text you specify.
As you can see, each live template has its own template abbreviation. However, the abbreviation must be unique only within a group. The same abbreviation may be used in several different groups: as a simple template, a parameterized template, and a surround template.
Regarding terminology, Live Templates support two variables whose behavior can’t be modified:
- $END$ indicates where the cursor will be placed after applying the template.
- $SELECTION$ will surround the selected text with custom live templates, just like it would with HTML tags.
Variables within templates are declared in the following format: $$. Each variable can be defined by custom or predefined functions in the Expression field, and it can accept some default value for cases when the expression fails to evaluate.
Note that a default value of a variable is an expression that can refer to other live template variables. It might be a macro_call (e.g. capitalize), a reference to another variable (e.g. VAR1), or a string literal (e.g. “Hello”).
After a template is expanded, variables appear in the editor as input fields. Use the Move Up / Move Down buttons in the Edit Templates Variable dialog to change the order of variables in the list. The order in this table shows the order in which GoLand will switch between input fields when the template is expanded in the editor.
Congratulations, now you know everything you need to create your own custom template with ease. The theory is over, so let’s get to practice!
For example, we want to have a custom template for connecting to our database with a specified password, hostname, port, and the name of the database to connect to.
- Go to Preferences | Editor | Live Templates and click + to add a new live template.
- Enter an abbreviation – you’ll use every time you want to invoke the template. For this one, we’ll go with sqlconnect.
- Add a description that will help you remember what the template does. Invoke the Quick Documentation popup (F1 on macOS or Ctrl+Q on Windows/Linux) to look for the template code snippet.
- Provide the code that should be generated in the Template text field. To specify where the cursor should appear, use the $END$ variable. In our case, we’ll want to have the cursor at the end of the expression.
- Click the Edit Variables button to add expressions to variables and specify the default values if it’s needed. In our case, we’ll only need to set the default values.
- Don’t forget to define contexts in which you are going to employ a template (Such as Go, Go Structs, CSS, JS, etc.).
- Finally, choose how you want to insert the code: with Enter, Tab, or Space.
- You’re done. Good job!
That’s all. Go ahead and create as many custom live templates as you need, to get the routine out of the way and reach for the stars!
Subscribe to Blog updates
Does Machine Learning in Go Have a Future?
In this article, you'll explore the challenges of actively using Go for Machine Learning.
OS in Go? Why Not?
In this article, you'll learn why languages like C have a stronghold over OS development and whether writing an OS using Go is possible.
Comparing database/sql, GORM, sqlx, and sqlc
This article compares the database/sql package with 3 other Go packages, namely: sqlx, sqlc, and GORM. The comparison focuses on 3 key areas – features, ease of use, and performance.