Scratch files
Create: CtrlAltShiftInsert
Sometimes you may need to create temporary notes or draft up some code outside the project context. Instead of switching to a different application, you can use scratch files and scratch buffers.
note
Scratch files and buffers are not related to a specific project. They are available for any project that you open with the specific IDE instance.
Scratch files are fully functional, runnable, and debuggable files that support syntax highlighting, code completion, and all other features for the corresponding file type. For example, while working on one project, you may come up with an idea for a method that you could later use in another project. You can create a scratch file with a draft of the method, which is not stored in your project directory but can be opened when you're working on any other project. You can use scratch files to draft code constructs, HTTP requests, JSON documents, and so on.
Scratch buffers are simple text files without any coding assistance features. Scratch buffers can be used for simple task lists and notes to yourself. They are also not stored in the project directory and can be opened from any other project. You can create up to five scratch buffers with default names, which are rotated and reused by clearing the content.
Go to File | New | Scratch File or press CtrlAltShiftInsert. Alternatively, in the Project tool window , right-click any area and navigate to New | Scratch File.
Select the language of the scratch file.
Scratch files of the same type are automatically numbered and added to the Scratches and Consoles directory of the Project view.
When you create a Go scratch file, GoLand automatically adds a
main
package and amain()
function.
Alternatively, you can create a new scratch file with the contents of the current selection in the editor. Select some text or code, press AltEnter and then select Create new scratch file from selection. GoLand will attempt to detect the language of the selected fragment and use the appropriate type and extension. If it can't detect the language of the selection, GoLand will create the file with the same type and extension as the original file.
tip
GoLand numbers scratch files sequentially starting from 1. If you close a tab with an empty scratch file, GoLand deletes it.
There is no dedicated menu item for the action to create a new scratch buffer, but you can use the Find Action popup CtrlShift0A and run the New Scratch Buffer action.
You can also add a shortcut for the New Scratch Buffer action as described in Keyboard shortcuts.
GoLand creates a text file named buffer1.txt.
The next scratch buffer you create is named buffer2.txt, and so on up to buffer5.txt. When GoLand reaches that limit, it will recreate buffer1.txt and suggest clearing the content for it. If you want to make sure GoLand does not clear the scratch buffer after you have five, you can rename it.
Open the Project view and expand Scratches and Consoles | Scratches.
tip
Consoles are used to compose and execute SQL statements for databases defined in GoLand as data sources. This functionality is available only when the Database Tools and SQL plugin is enabled. For more information, refer to Query consoles.
Use the Find Action popup CtrlShift0A to invoke the Show Scratch Files action and view all available scratch files in a popup:
By default, GoLand stores scratch files and buffers in the IDE configuration directory under scratches. They are available from any IDE and project that uses this configuration directory.
To change the location of the Scratches and Consoles directory, use the idea.scratch.path
platform property. To change the location of just the Scratches directory, use the idea.scratch.path/scratches
platform property. For more information, refer to Advanced configuration.
If a scratch file grows into something that you want to use in your project, move it into the desired directory of your project structure.
Open a scratch file in the editor or select it under the Scratches and Consoles | Scratches directory in the Project view, press F6, and select the target directory in your project.
Drag a scratch file from the Scratches and Consoles | Scratches directory in the Project view to the target directory in your project.
Select a scratch file in the Scratches and Consoles | Scratches directory in the Project view and press Ctrl0X, then select the target directory in your project and press Ctrl0V.
In the Project tool window, open the Scratches and Consoles | Scratches.
Right-click a scratch file or buffer and then click Change Language.
Select the desired language. You can start typing the name of the language to narrow down the list.
Select any scratch file or buffer in the Project tool window and press Ctrl0Q to view quick documentation with the location, type, size, creation and modification date of the file.
Copy your code from a project file to a scratch file.
Press the Run button.
GoLand creates a Run configuration with the
-overlay
flag. This flag is used with a JSON file that maps file paths togo.mod
andgo.sum
in your original project.
You can run scratch files the same way that you run regular files. For more information about running SQL files, refer to Run SQL files.
To run queries in an SQL scratch file, you need to have a connection to a data source. For this, attach a scratch file to a session.
When you attach your scratch file to a session, GoLand resolves the objects in your SQL code. As a result, you can navigate from the SQL code to the database objects.
Press CtrlEnter or click
on the toolbar. In the Sessions window, select a session.
Click the <session switcher in the upper right corner of the window and select a session. The switcher menu includes sessions that are associated with the same language as your scratch file.
If you want to attach a session of another DBMS, change the scratch file language. To change a language, right-click any area in the open scratch file and click Change Language.
Right-click any area in the open scratch file and click Execute. In the Sessions window, select a session.
Select code that you want to run in the Go Playground.
Right-click the selection and navigate to Open In | Playground.
Click the Run icon (
) on the toolbar.
Note: if you run your code from the context menu or from the gutter, GoLand runs your code locally. Also, you can run your code locally by selecting the Run locally.
For more information about running code in the Go Playground, refer to Running applications in Go Playground.
Thanks for your feedback!