GoLand 2021.1 EAP 3 Introduces go:embed Support, Run Targets, Extract Type Refactoring, and More
It’s a new week, and we have a new GoLand 2021.1 EAP release to share with you. It has a lot of new and exciting features to try out.
What have we been up to?
- Support for //go:embed is now available for Go 1.16 or later (GO-10062)
- Initial Run Targets support
- Extract Type Refactoring (GO-17)
- Create Type quickfix generates missing fields (GO-7615)
- Other noteworthy changes
And of course, there are plenty of other fixes and improvements under the hood.
You can get the build via the Toolbox App, download it from our website, or use a snap package (for Ubuntu). Or from inside GoLand, select Automatically check updates for Early Access Program in Settings/Preferences | Appearance & Behavior | System Settings | Updates.
Support for //go:embed
Starting with Go 1.16, which is due to be released sometime soon, you’ll be able to embed files and directories into a Go binary at build time.
Let’s take a look at how we could do that.
Embedding works on a few types of variables: string, byte, and embed.FS. We also need to declare these variables at the top level of our package, which means that these variables cannot be contained within function bodies.
Finally, we need to add the
//go:embed directive for embedding to work.
You may notice in the screenshot that "resources" is highlighted a little bit differently than usual.
That’s because we didn’t stop at just adding a different color for this type of comment. In this case, "resources" is a reference to a folder in the project directory with the same name.
The IDE allows you to quickly navigate from references inside the embed directive to the files and folders in your project.
It can also display some useful information about them, such as the file size, and it can even allow you to rename them inside the code.
If you use Rename, Shift + F6, on the referenced file or folder from the Project Tool window, it will be renamed inside the embed directive.
And if you invoke Find Usages, Alt + F7 on Windows/Linux or Opt + F7 on macOS, then you’ll be able to find it inside your code too!
Did you notice that you can also use completion inside the embed directive?
To prevent errors that may occur when using this new syntax, we added a few inspections too.
If you reference a file or folder that doesn’t exist, the IDE will tell you about it.
Do you want to try to embed files in a struct type or other unsupported types? The IDE will also catch that and inform you about it.
What if you forget to add the "embed" package to your import list? Use Alt+Enter to bring up the Show Context Actions menu and select Import embed from the available quick-fixes list. It will be smart enough to import it for side-effects only if you don’t’ use an
embed.FS type already.
GoLand has only been capable of building and running code on the machine installed on until now.
However, many users have told us that they’d like to run their code closer to the environment that they’ll end up deploying in.
We’ve listened to this feedback, and today we are happy to show you the first version of the Run Targets EAP.
This feature allows you to develop on the local machine, with all the source code available to you locally, while targeting remote computers for running that code.
Please remember that this feature is in the early stages of development, and it may evolve or change as we iterate over it.
At the moment, we support Docker, SSH, and WSL2 (Windows Subsystem for Linux).
Note In this first release, we support only the following scenario: the Build stage happens on the local machine while the Run and Debug stage occurs on the remote target. We are working to enable other use-cases. Stay tuned for more news on this!
Run Target for Docker
Let’s start with Docker support.
Head over to Run | Manage Targets… | + | Docker and select the Docker type from the list.
Then select whether you want to use an existing container image or pull one from a repository.
If you need to expose any ports or use any other flags for the container runtime, you can add them under Run options. By default, the IDE adds "–rm" which will remove the container after it stops, making cleanup easier.
Clicking Next will pull the container and allow the IDE to do an introspection of the build/run environment to detect where the "go" binary is, what the "GOPATH" is, and what version of Go is being used.
Finally, you can configure the container directory in which the IDE will place the sources and adjust any previously auto-detected features.
Now, let’s set up the Run Configuration from Run | Edit Configurations… and select the configuration that should run via Docker.
After changing the Run on field to match our newly created Docker Run Target, we can then click Ok and launch our Run Configuration.
The IDE will compile our binary on the local machine and run it via the Docker container we specified in our Run Target.
If you have feedback for this feature, head over the dedicated issue.
Run Target for SSH
Next on our list is support for targeting SSH remote configurations.
Like the Docker Run Target, the SSH functionality starts by configuring a new SSH Run Target via Run | Manage Targets… | + SSH.
We need to enter the hostname or IP address of the remote machine, the port on which SSH listens, and the username. Next, we need a password or SSH key-pair to connect to the target.
Once those are provided, the IDE will again try to infer the environment from the machine.
Note: To ensure you get the best possible performance for transferring data between the local and remote machines, you should have rsync on the remote target.
As with Docker, changing the Run Configuration to target the SSH Run Target allows us to run the binary against the remote machine.
Note: Binaries built on Windows 10 and running on UNIX-based systems are not currently supported using SSH Run Targets. We’ll address this in a future build.
Run Target for WSL2
For users running Windows 10, Remote Targets can use WSL2 (Windows Subsystem for Linux), in addition to SSH and Docker.
This works by adding a WSL target from Run | Manage Targets… | + | WSL.
You can configure a name for this target, then select which WSL2 Linux Distribution to use.
Just add the path to the Go executable and GOPATH used under the selected Linux distribution, and you are good to go.
We are just a couple of steps away from making this work. Edit the Run Configuration and make its target the new WSL2 configuration created above.
That’s all you need to do. When you run your configuration, it will look and feel the same. GoLand will take the necessary steps to use WSL instead of Windows to run your code seamlessly under the hood.
At the moment, the build step will take place on the Windows side.
This takes advantage of Go’s cross-compilation capability.
As this feature is still in its early stages, you may encounter some limitations.
For example, one of them involves projects that rely on CGO; cross-compilation is not as easy to do in this case.
We’ll support additional scenarios, such as compiling on the remote side, to address these shortcomings.
If you have feedback for this feature, head over the dedicated issue.
Extract Type Refactoring
Have you ever wanted to extract a type from an existing one?
Or to convert that anonymous structure type to a named type?
Extract Type refactoring, Ctrl + Alt + Shift + T on Windows/Linux or Ctrl + T on macOS, will do just that.
Create Type quick-fix generates missing fields
Before this release, GoLand would offer to create a type when it noticed you were using one that was not yet defined.
However, in the case of struct types, it would only create the type itself, leaving the task of creating the fields to you.
We’ve addressed this problem, and now GoLand can now automate this step for you.
JSON Lines support
GoLand now supports the newline-delimited JSON Lines format used for working with structured data and logs. The IDE will recognize the .jsonl, .jslines, .ldjson, and .ndjson file types.
In this format, files contain several lines, and each new line is a JSON object separated by a newline character. Commas are required at neither the beginning nor end of lines, and there is no need to enclose the entirety of the file contents in square or curly braces.
Quick access to recent projects on Windows
You can now access your recent projects by right-clicking on the GoLand icon on the taskbar or on the Start menu in Windows.
Better tooltips display
Tooltips are now shown one after another to prevent them from overlapping.
We are continually working to make our IDE more accessible to all users. In this EAP, we’ve added more labels to UI elements on the Welcome screen, the Project Structure view, and the VCS log. These elements and gutter icons are now read out correctly when a screen reader is enabled. We are also working on better accessibility support for macOS users.
Visual Stage Separator in multistage Dockerfiles
It’s now easier than ever to read multistage Dockerfiles thanks to the separator lines that define boundaries between one stage and another. No more guesswork is needed to determine when a stage ends and when one begins.
You can now select the font variations from the Settings/Preferences | Editor | Font settings panel.
Some other noteworthy bug fixes include:
- Buildkit is now supported as an experimental feature. You can find it in the Run Configuration of the Docker type Run Configurations.
- The IDE can now automatically detect case-sensitivity per directory. You don’t need to customize this setting manually. (IDEA-135546)
- Fixed how floating windows are displayed. (JBR-1752)
- The Find in Path dialog now closes when you click on a code area. (IDEA-170038)
- Fixed IDE crashes on macOS and at startup. (JBR-2947), (JBR-3066)
If you learn about even more issues that have been addressed, take a look at our release notes.
Our team is very excited about this release, and we hope you’ll find it equally exciting and useful.
As usual, your feedback is invaluable to us, and we’d like to hear your thoughts about these features and about what else we can do to make your experience even better. Don’t hesitate to contact us by writing in the comments section below, opening an issue on [our tracker](https://youtrack.jetbrains.com/issues/Go), dropping us a line in the Gophers Slack #goland channel, or pinging [on Twitter](https://twitter.com/GoLandIDE) or social media platforms.