Automating Go Workflows with GoLand External Tools
Learn how to integrate custom Go CLI tools into GoLand using External Tools to automate code generation and formatting without leaving the IDE.
25 Jul 2025, 08:48 UTC

The Problem: Repetitive CLI Tasks in the IDE
Go developers often rely on custom CLI tools for repetitive tasks: generating boilerplate for a specific framework, running a custom linter, or triggering a specialized formatting script. Switching between the IDE and a terminal breaks flow and introduces manual errors, such as running a tool against the wrong file or forgetting to pass the correct path.
The solution is implementing External Tools in GoLand. This feature allows you to map a system binary to an IDE action, automatically injecting context—like the current file path or selected text—as command-line arguments.
Prerequisites
- GoLand installed and configured with a working Go SDK.
- A compiled Go binary or a script (e.g.,
.shor.bat) that accepts command-line arguments. - The binary must be located in your system
PATHor you must have the absolute path to the executable.
Configuring the External Tool
To integrate a custom tool, follow these steps to define the execution parameters and context macros.
1. Define the Tool
- Navigate to Settings (
Ctrl+Alt+S/Cmd+,) > Tools > External Tools. - Click the + (Add) icon.
- Fill in the Name (e.g., "Custom Code Gen").
- In the Program field, enter the path to your binary. Use the absolute path if the tool is not in the system
PATH.
2. Parameterize with Macros
The Arguments field is where you connect the IDE's state to your tool. GoLand uses macros (placeholders) to pass dynamic data. Common macros include:
$FilePath$: The full path to the file currently open in the editor.$SelectedText$: The text currently highlighted by the user.$ProjectFileDir$: The root directory of the current project.
Example Configuration: If you have a tool that generates a mock for a specific struct, your configuration might look like this:
Program: /home/user/bin/gomock-gen
Arguments: -source="$FilePath$" -struct="$SelectedText$"
Working directory: $ProjectFileDir$
3. Execution and Output
Under the Advanced Options, ensure "Open console for tool output" is checked. This ensures that stdout and stderr from your Go binary are captured in the GoLand tool window for debugging.
Practical Example: File-Based Metadata Generator
Imagine a tool that reads a Go file and generates a corresponding .meta file for documentation. To implement this, you would configure the tool as follows:
| Field | Value | Purpose |
|---|---|---|
| Program | meta-gen |
The CLI binary name. |
| Arguments | --input "$FilePath$" |
Passes the current file; quotes prevent errors with spaces in filenames. |
| Working Dir | $ProjectFileDir$ |
Ensures output is written relative to the project root. |
Verification and Diagnostics
To verify the tool is functioning correctly:
- Open a Go file in the editor.
- Right-click anywhere in the editor > External Tools > [Your Tool Name].
- Check the Run tool window at the bottom of the IDE. You should see the output of your binary.
- Path Test: Rename a file to include a space (e.g.,
test file.go) and run the tool. If it fails, ensure you wrapped$FilePath$in double quotes in the Arguments field.
Advanced Integration
Keyboard Shortcuts
To avoid the right-click menu, go to Settings > Keymap. Search for your tool's name under the "External Tools" section and assign a shortcut (e.g., Ctrl+Shift+G).
Before Launch Tasks
You can chain this tool to run automatically before a build or test. In your Run/Debug Configuration, find the Before launch section, click +, and select Run External Tool.
Limitations and Risks
- UI Blocking: If the external tool takes several minutes to run, it may appear as though the IDE is lagging, though the tool typically runs in a separate process.
- Shell Injection: Be cautious when using
$SelectedText$in tools that execute shell commands. A user could potentially inject malicious flags or commands if the tool does not sanitize input. - Environment Variables: External tools inherit the environment of the IDE. If your tool requires specific
GOROOTorGOPATHsettings different from the IDE's, you must define them within the tool's environment settings.
Rollback
To remove the integration, go back to Settings > Tools > External Tools, select the tool from the list, and click the - (Remove) icon. This removes the IDE mapping but does not delete the binary from your system.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.