Chase Mao's blog

Golang Development Enviroment: Win, WSL2, Goland

2021-07-01

Introduction

In this article, I will guide you through setting up a Golang development environment using WSL2 on Windows and Goland. This setup is driven by three key considerations:

  1. Device Preference: While Linux is the preferred development environment for backend development, I am a long-term Windows user. Therefore, I need to explore alternative options that allow me to leverage Linux’s capabilities without leaving the Windows ecosystem.
  2. Remote Development Limitations: Goland is the preferred IDE for Golang due to its intelligent code completion, implementation, and refactoring features. However, remote development with Goland is not ideal. It primarily functions as SFTP file synchronization, and the code suggestions depend on the local environment (system environment, dependency packages, etc.), making it inconvenient. Thus, remote development is not a viable option.
  3. Exploring WSL: I discovered Windows Subsystem for Linux (WSL), which integrates a Linux subsystem within the Windows operating system. WSL provides a complete Linux kernel and supports most Linux functionalities. It is even touted as the best Linux distribution.

By the end of this article, you will have a robust Golang development environment set up on your Windows machine using WSL2 and Goland, combining the best of both worlds.

Enable WSL2

  1. Enable Windows Features:

    • Open the Control Panel.
    • Navigate to “Programs” > “Turn Windows features on or off”.
    • Enable “Hyper-V”, “Windows Subsystem for Linux”, and “Virtual Machine Platform”.
    • Restart your computer.
  2. BIOS Configuration:

    • Enter the BIOS setup during the boot process (usually by pressing a key like F2, F10, or Delete).
    • Enable virtualization technology (Intel VT-x or AMD-V).
    • Save and exit the BIOS setup.
  3. Set WSL2 as Default:

    • Open PowerShell as an administrator.
    • Run the command: wsl --set-default-version 2.
    • Follow the prompts to download and install the Linux kernel update package.
  4. Install Ubuntu:

    • Open the Microsoft Store.
    • Search for “Ubuntu” and install it.
  5. Configure Default User:

    • Open PowerShell.
    • Run the command: ubuntu config --default-user root.
    • This ensures that all WSL operations are performed with root privileges.
  6. Change Ubuntu Sources:

    • Open the Ubuntu terminal.
    • Edit the sources list file: sudo nano /etc/apt/sources.list.
    • Replace the existing sources with a mirror that is appropriate for your region.
    • Save the file and run sudo apt update to update the package lists.
  7. Install Golang:

    • Download and install the appropriate version of Golang from golang.org.

Setting Up Goland on Windows for Development

  1. Install Golang and Goland on Windows:

    • Download and install the appropriate version of Golang from golang.org.
    • Download the latest version of Goland from JetBrains.
  2. Set Up Golang in Goland:

    • Under the GO section:
      • Set GOROOT to the path where Golang is installed on Windows.
      • Ensure GOPATH matches the GOPATH used in WSL. It makes sure that no matter fetch dependancy from Windows or WSL2, the dependancy will be downloaded and share in the same dirctory.
  3. Configure Terminal:

    • Under the Tools section:
      • Set the Terminal Shell Path to wsl.exe to use the WSL2 terminal in GoLand.

Conclusion

With WSL2 and GoLand both set up, you can now develop Golang programs using GoLand on Windows while leveraging the powerful tools available in Linux through WSL2.