GoLand 2021.3 Help

WSL

WSL (WSL 2) - Windows Subsystem for Linux - is a compatibility layer for running Linux binary executables natively on Windows 10. Currently, it supports several Linux distributions, such as Ubuntu, OpenSUSE, and SLES.

Configure WSL

  1. Download and install a WSL distribution (for instance, Ubuntu) from Microsoft Store.

    For this step, be sure to use Windows 10 with the latest “Fall Creators Update” (minimum version 1709, build 16299.15). See the official guide Install the Windows Subsystem for Linux for instructions.

    To work with WSL 2, your Windows version should be 10 build 18917 or later. Follow these instructions to switch the distributive.

    Note that GoLand does not support legacy WSL, which you may have installed before upgrading your system to the build 16299.15 or later of Windows 10. In this case, you need to update your WSL distribution.

  2. Run Ubuntu.

    Upon the first launch of Ubuntu, the system may prompt you to enable the Windows optional feature. In this case, you need to do the following:

    • Open Windows PowerShell as Administrator and run

      Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    • Restart your computer.

  3. From your home directory, download the tarball.

    wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz
  4. Extract the archive to /usr/local.

    tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz

  5. In your home directory (cd ~), create the go folder.

    mkdir go
  6. Configure GOPATH and GOROOT.

    export GOROOT=/usr/local/go
    export GOPATH=$HOME/go
    export PATH=$PATH:/usr/local/go/bin

You can create a project and store it in the WSL environment, open one from the WSL file system, and develop your projects further in WSL.

Create a new project in the WSL file system

  1. Install the needed Go version to the WSL environment.

  2. On the welcome screen, click New Project.

  3. Click the Browse button and navigate to the directory in WSL where you want to create a new project.

    In the Select Base Directory dialog, you can click the New Folder button (the New Folder icon) to create a folder for a project.

    The project location for WSL will show the absolute path starting with \\wsl$ to the WSL file system.

  4. Click Create.

    GoLand creates a project located in WSL and you can develop and build your project inside the WSL environment without leaving the IDE.

    create project folder in wsl

Open a project stored in the WSL file system

In GoLand, you can directly open a project stored in the WSL file system and work with it like with any other project.

  • Click the Browse button and navigate to the directory in the WSL file system that you want to open. Also, you can type the path to the \\wsl$ project location manually.

    open existing project in wsl

When you run code in your project, it is run in WSL. If you open the run configuration, note that GoLand refers to WSL as local machine.

Run configuration

The results of the code execution are displayed in the Run tool window.

Configure Terminal in WSL

  1. To run Terminal in WSL configuration, open project Settings/Preference (Ctrl+Alt+S) and go to Tools | Terminal.

  2. Type wsl.exe in the Shell path field and click OK.

    Terminal settings
  3. Switch to the Terminal tool window and type any command to inspect the output.

    Terminal in WSL configurations

Use the WSL environment for a project

You can create or open your project locally on Windows OS with local JDK and then run your compiled code in WSL using run targets. This might be helpful for the cross-platform development.

Change run target to WSL

Create a run configuration for WSL

  1. From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10, then 0.

  2. In the Run/Debug Configuration dialog, click the Add button on the toolbar or press Alt+Insert.

  3. From the Add New Configuration window, select the run/debug configuration that you want to create for running a target. It can be Go Build or Go Test.

  4. From the Run on menu, under the New targets section, select WSL to add a WSL target.

    select wsl from run on list
  5. In the New Target wizard, select your Linux distribution from the Linux distribution list. If the distribution was detected automatically and the introspection passed, click Next.

    introspection of wsl target
  6. Check that the Go runtime configuration for WSL was detected correctly and click Finish.

    Alternatively, type your own settings.

    • Go Executable: the path to the Go executable (for example, /usr/local/go/bin/go)

    • GOPATH: the path that defines the root of your workspace. A root directory must contain bin, pkg, and src subdirectories. (for example, /home/goprojects)

    • Version: a version number of your Go SDK (for example, go1.15.8 linux/amd64). GoLand detects this information automatically by running go version.

      • Additional settings: a group of settings that allow you to set directories for source code files and executables.

        To run on a target, GoLand needs to upload to a target the following entities: source code files, files from a working directory, compiled binaries, and delve. If you do not specify paths in Additional Settings, the IDE will create random directories to store previously-mentioned files. See the following screenshot with randomly-generated directories for source files and executables.

        randomly-generated directories

        You can specify the following options:

        • Project sources directory: a directory on a target where source code files are uploaded.

        • Compiled executables directory: a directory on a target that is used to store executables that your code produces.

    check Go runtime configuration for WSL
  7. From the main menu, click Run and select the necessary run configuration or press (Shift+F10) to run your code and check the output in the Run tool window.

    Run tool window: WSL output
Last modified: 19 March 2022