5 IntelliJ Tricks for Gophers

intellij tricks for gophers

A small collection of IntelliJ tips and tricks for getting more out of your day or simply doing the same amount for work with less effort.

1. Install the IntelliJ Go plugin from here

Enough said.

2. Project structure

  • I recommend setting the base of your project as “source” and then excluding any directories you don’t need for development work.
  • You can then change the “Project” view to “Packages” instead of “Project” for a distraction free file list.
  • To manage the folders, use right click on the middle panel on the screen below.

  • project-structure

    3. Editor defaults

    This one won’t necessarily speed you up but it should make life a little more comfortable.

    Use the “Code Style” preferences, make a custom scheme and set all the tab and indent settings to 4. This will stop the code jumping around once it’s formatted by the regular go tools.


    code-format

    4. Testing (and formating)

    While I am not a TDD fanatic, I do like to run my tests quite often. I use the “Run Test” feature in IntelliJ to also format and fix the imports. To do this:

  • start with an already defined “Run Configuration”, click the + button on the “Before Launch” panel and
  • select “Run External Tool”
  • test-external-tools

  • Now, the setup of external tools will be a little weird (or at least it seems so to me); You will be presented with this dialog:

  • external-tools-dialog

  • The strange part – To select a tool to run, it doesn’t seem to matter what is “checked” only what is “selected” when you click OK.
  • So to add an external tool we use the “+” button on the bottom of the panel and then complete the dialog that appears.
  • Here are the settings for running GoImports
  • test-external-tools-goimports

  • Here are the settings for running GoFmt
  • test-external-tools-goimports

  • This is what it should look like when it’s all done:
  • test-external-tools-done

    5. Live Templates

    This is perhaps the best way to make yourself more efficient (excluding learning and using keyboard shortcuts). Live Templates allow you to specify “shortcuts” that you can quickly turn into blocks of code.

    For example if you type “forr” and press Tab it will insert:

    The thing most likely to trip you up when creating your own Live Templates is the “Applicable in Go: xxx” setting at the bottom of the panel (see below).

    Generally, to insert “top level” elements like structs & interface definitions or functions then use “Go file”. To insert chunks of code inside a function use “Go block”. I have not personally used the other two options.

    test-live-template-edit-tdd

    Here are some others that you might like:

    “printf”

    “test”

    “tdd”

    I like to use the Testify lib for my tests so I find these particularly useful.

    “ae”

    an

    ann

    at

     

    Happy Coding!


    If you like this content and would like to be notified when there are new posts or would like to be kept informed regarding the upcoming book launch, please join my Google Group (very low traffic and no spam).