golang[74]-goland-技巧

Implement Interface

Implement Interface is a hidden gem that can be found by using the common Intellij keystroke combo ⌥ (Option/Alt) + Enter on the struct that you wish to implement the interface. This will open a search window, and you can type the interface that you wish to implement.

Extract Interface

I wish this had a great keyboard combination, but it doesn’t currently by default. If you wish to extract an interface, then you need to Right-Click on struct → Refactor → Extract → Interface. Once the menu pops up it asks you more information, name, file to add to, package…etc. I still use this quite often.
you can consider Ctrl+T 6 (Refactor this) as a shortcut for Extract interface

Live Templates

Live templates are awesome for quickly generating some code that you commonly use. In general, you type in a few characters and then hit tab. Some built in live templates are forr which generates a for range loop (you need the extra “r”) and err which allow you to handle errors quickly. These are really powerful because you can also create your own live templates. For example, if you have a common logging framework when you logged “info” you could create logi as your own template which would generate your “log.info” statement. If you have a hard time remembering these, just remember one keyboard combo: ⌘ (Command) + j — this will show you all available templates within the context of the block of code where your cursor is at. Enjoy these examples below:

forr — for range loop
To execute this live template, just type “forr” + tab

3b. err — check for error and handle
To execute this live template, just type “err” + tab

Fill Struct

I am not exactly sure when this was released, but it is glorious. If are familiar constructors in Java or C#, then you were used to a little IDE help when you instantiated a new class. This gives you that same power (albeit a little more clunky). In this case, if you have a struct that you are creating, then if you put your cursor between the brackets {}, then hit ⌥ (Option/Alt) + Enter, it will give you the option of “Fill Struct” or “Fill Struct Recursively”. Choose one. Save your typing for some other fun code, and let this thing do its work!

Generate test cases/test file

This is probably my favorite. I am pretty passionate about writing testable code and unit testing in general. I really think that writing testable code using SOLID principles will help you design better and more maintainable code. I found this nugget pretty early in my golang learnings, but it is definitely my most used feature. If you put your cursor on a method, type ⌘ (Command) + n , and this will bring up the “Generate” menu, and from here you can select “Test for function” or “Test for file” and if the _test.go file doesn’t exist, it will create it for you along with the outline of parameterized/table tests for your functions. Pretty awesome.

Refactorings

Refactorings is another fundamental need one may have when making changes to a program.

With GoLand, you can extract a function from an expression at the caret, or from a group of selected statements, by using the dedicated Extract action (Alt+Ctrl+M for Windows/Linux and Alt+Cmd+M for macOS), or selecting the Method option in the generic Refactor This popup (Ctrl+T):

The same refactoring is available for variables (Alt+Ctrl+V for Windows/Linux and Alt+Cmd+V for macOS) and constants (Alt+Ctrl+V for Windows/Linux and Alt+Cmd+V for macOS).

You can inline a constant, or variable at the caret by using the dedicated Extract action (Alt+Ctrl+M for Windows/Linux and Alt+Cmd+M for macOS), or select the Inline option in the generic Refactor This popup (Ctrl+T). The IDE will update all references to the symbol and replace them with the value:

One thing that you do more often than write code is read code. GoLand helps here as well. For example, the IDE lets you instantly navigate to an interface implemented by the type at the caret with one click: either by clicking the icon on the left side of the editor, using the dedicated Show Interfaces (Ctrl+U for Windows/Linux and Cmd+U for macOS) action, or by using the corresponding intention action:

A similar icon, dedicated action (Show Implementations via Ctrl+B for Windows/Linux and Cmd+B for macOS) and intention action can navigate you to types that implement the interface at the caret.

Every time you see a popup with a list of symbols, be it usages, implementations, implemented interfaces or completion suggestions, you can call Quick Definition (Ctrl+Shift+I for Windows/Linux or Cmd+Y for macOS) or Quick Documentation (Ctrl+Q for Windows/Linux and Ctrl+J for macOS) to have another popup open aside it with the definition or documentation for the selected symbol:

Navigate back/forward ⌘[ ⌘]

Expand or collapse a code block in the editor ⌘+ ⌘- shift

Show implementation ⌥⌘B ⌘B

Quick definition ⌥Space

Search everywhere Double Shift
importtant ⌘E
Navigate to type hierarchy ⌃H

参考

https://medium.com/@andrey_cheptsov/golands-take-on-go-development-7d2611b14b99
https://www.jetbrains.com/help/go/discover-goland.html