All About Code Completion in GoLand
Code completion is one of the greatest benefits of using an IDE: it reduces the number of things that you need to memorize and helps avoid typos and other common mistakes. It is useful for both professional developers who can write clean code faster, and for beginners who get easier ways of learning programming languages.
In this article we’re going to have a look at the many code completion features in GoLand, and how to effectively use them.
Type-aware completion
GoLand has two types of type-aware completion: Smart Code Completion and Basic Code Completion, and the way these features work is one of the many things that make GoLand differ from other IDEs and code editors.
Basic Code Completion
Basic completion provides a list of all symbols available at the current caret position. The list will include the usual mix of type, interface, and method names, keywords, and also Live Templates. Symbols from not-yet-imported packages are also offered, with autoimport on-the-fly when you select them.
How to use
Basic completion pops up as you start typing code and GoLand recognizes an opportunity to complete something for you, but you can invoke it anytime by pressing ⌃Space on macOS or Ctrl+Space on Windows/Linux.
Quick tip: Basic Code Completion is also available for non-English symbols.
Smart Code Completion
Smart Code Completion is the king of code completion in GoLand, and here’s why: it gives you a list of the most relevant symbols that are applicable in the current context.
How to use
Press ⌃⇧Space on macOS or Ctrl+Shift+Space on Windows/Linux.
Cyclic Expand Word a.k.a. Hippie Completion
It’s a type of code completion that analyzes your code in the visible scope, helps you to complete any word from any of the currently opened files, and provides you with suggestions from the current context, which can save you plenty of time when, for example, you are working on boilerplate code.
How to use
- Press ⌥/ on macOS or Alt+/ on Windows/Linux to search for matching words before the caret or choose Code | Completion | Cyclic Expand Word.
- Press ⌥⇧/ on macOS or Shift+Alt+/ on Windows/Linux for searching words after the caret or choose Code | Completion | Cyclic Expand Word (Backward).
Pro tip: hold the Alt key down and keep pressing / until you get to the word you need.
Method-like completion for functions
Imagine you are new to Go and want to split a string. You know that there must be a function for that, most likely it’s called ‘Split’ and accepts a string as the first argument, but you don’t know the exact name of the function and its package. Here’s where method-like completion comes in handy, offering you a list of all suitable functions, automatically importing it upon selection.
How to use
Hold down the Ctrl key and press Space twice.
Postfix Completion
Postfix Completion lets you transform an expression into another one, based on the postfix you specify after the dot, the current context, and the expression type. For example, you can turn a bool expression into an if statement, or create a pointer to an expression. If anything goes not as planned, everything can be reverted via simple Ctrl+Z.
Pro tip:
- Postfix Completion options are displayed as a part of the Basic Completion suggestions list. Press ⌘J on macOS or Ctrl+J on Windows/Linux to see a full list of the Postfix Completion templates applicable in the current context.
- You can change the names of all Postfix Completions in the settings dialog.
- In the same dialog, you can also disable the templates you don’t need, or even turn off Postfix Completion completely.
How to use
Open the Postfix Completion page via Settings/Preferences | Editor | General to see the complete list of supported templates. Learn, play, and of course, enjoy!
Live Templates
Live Templates are great for reducing the time you spend writing frequently used portions of code, like variable and loop declarations, or test functions.
Of course, you can edit, rename, turn off, or remove any of the predefined Live Templates, but what about adding your own? That is quite an interesting topic that we will cover in the upcoming articles about GoLand features.
How to use
Open the Live Templates page in Settings/Preferences | Editor | to see the full list of already supported templates. Learn, play, and again, enjoy!
Complete Current Statement
GoLand can complete statements and add required trailing commas for any composite literals (like struct, slice, etc.), and even place caret in a position where you can just start typing the next statement.
How to use
To invoke this action, press ⌘⇧↩ on Mac or Ctrl + Shift + Enter on Windows /Linux.
Partial Match Completion
GoLand provides completion suggestions when you typed only a part of a word (any part, even characters from somewhere in the middle).
Also, case-insensitive CamelHump completion is supported, so you can just type the capital letters along with a number of subsequent characters. For example, type ReWrCl to get io.ReadWriteCloser.
How to use
Works automatically, you don’t have to do anything.
Parameter Info
The Parameter Info popup displays available method and function parameters.
How to use
Popup appears automatically in about one second after you type an opening bracket after method name or you can press ⌘P on macOS or Ctrl+P on Windows/ Linux anytime to invoke it.
Pro tip: You can adjust the popup delay via Preferences/Settings | Editor | General | Code Completion.
Completion for non-imported Go Modules
GoLand provides code completion suggestions for all modules in the modules cache as you type. This means that, even if a module has never been imported and used in your project, GoLand is still aware of it and will take care of you.
How to use
Completion will work automatically as you start typing a package name.
Code completion in Evaluate Expression and Watches
You got it right, debugger also has code completion! GoLand provides suggestions list when you evaluate the state of your application in a debugging session.
How to use
As you start typing in the Evaluate Expression or Watchers dialogs, code completion will appear immediately.
Code completion in Run Configurations
When you’re editing Run/Debug Configurations, code completion will automatically appear to help you, for example, type a package name.
Completion with the Tab key
Tab completion is a small, but handy feature that makes it a little easier to complete symbol names. If used instead of Enter to accept a suggestion from list, it overwrites the identifier at the caret.
Code Completion in Language Injections
GoLand provides code completion and analysis in injected strings for C, SQL and databases statements, JavaScript, CSS, HTML, XML, and RegExp, etc.
How to use
Place the cursor within the literal (between the quotation marks) and press ⌥⏎ on macOS or Alt+Enter on Windows/Linux, select Inject language or reference, choose which one of supported injection language you want to use and start typing, auto-completion will appear instantly.
Exclude from imports and completion
In Settings/Preferences | Go | Imports you can add as many as you want import statements that will be excluded from code completion and auto-import.
That’s all for today! Please share your thoughts, feature requests or any issues you encounter through the issue tracker, in the comments below, or with a tweet at us.