How to Setup Go in Visual Studio Code

Robert Keith Rippetoe
6 min readMay 15, 2024

--

Looking to get started coding with Go? Want to move on from online coding platforms to your own local environment? This guide will help you do so even if you are a complete beginner with no previous experience.

Step 1: WSL (Windows Only)

Terminals are incredibly powerful and the staple of programmers. They allow you to quickly alter files, change working directories, and interact with powerful CLI (Command Line Interface) tools. An unfortunate problem can be that while Windows is an incredibly user friendly and popular OS, it has relatively terrible terminal support that doesn’t share the same commands as Unix systems. To fix this, Windows recently began supporting WSL (Windows Subsystem Linux), which will allow you to use Unix terminal commands without the need for a new computer or Linux dual boot.

  1. Open your toolbar in the bottom left, type ”power”, right click the result Windows Powershell with Run as Administrator
Powershell is also a terminal, but most prefer avoiding using it if possible

2. Type and enter

wsl --install

This will default to WSL 2.0, a newer version of WSL.

3. After waiting for installation, select your username and password, which are separate from your Windows username and password.

4. Restart your computer

You will know you have done this correctly if you can follow steps 1 and 2 and receive a message like this.

Congratulations! Feel free to try out common Unix commands like ls to list the files and folders in your current directory, cd to change directories, and pwd to show your current working directory, the directory you are executing your commands from.

Step 2. Install Visual Studio Code

Visual Studio Code is one of the most popular IDE’s (Integrated Developer Environments) used today, and has built in compatibility with WSL for Windows users.

1. Start by visiting the official VS Code download page, https://code.visualstudio.com/download.

2. Select the option that corresponds to your operating system. Downloads on average should take a few minutes depending on internet connection.

3. For Windows: You are most likely using an x64 system architecture, but you can check by using the toolbar in the bottom left, visiting System Information, and checking System Type under the System Summary tab. After the download is finished, select the User Installer corresponding to your system and follow the installation prompts.

For Linux: Choose the download corresponding to your Linux distro and system architecture. To find your system architecture, type uname -i. If the output says “x86_64”, you have a 64-bit installation. If it says “i368”, you have a 32-bit installation, and otherwise it should be arm64.

  • To install .deb files, use
sudo apt install FILE_NAME.deb

from the directory you downloaded the file.

  • To install .rpm files, use
sudo rpm -i [FILE_NAME.rpm] 

from the directory you downloaded the file.

For Mac: Select About This Mac in the top left of the screen. If the text entry under Chip header contains Apple, select Apple Silicon. If the text contains Intel, select Intel chip. Otherwise, select universal. After the download, select the file from your web browser and follow the prompts.

After completing this step, ensure you have Visual Studio Installed by running the program as you would any other program on your computer. The result should look like this except, without WSL extension installed yet:

Step 3. Add the Go Extension

Visual Studio uses extensions to allow it to install plugins that add new functionality, such as being able to read a new coding language.

  1. Select the tab on the left side of your screen with several boxes stacked on top of each other.

2. Type in go in the Extensions MarketPlace in the top left. From here select Install Workplace Extension.

3. (Windows Only) Follow the exact same steps except with the WSL extension. After completing this step, press ctrl+shift+P and type

WSL: Connect to WSL 

to shift Visual Studio Code to use Linux instead of Windows!

Once finished, the options under your extension should now read disable or uninstall rather than install. With these installed, your IDE will now automatically identify any Go files you open in the future!

Step 4: Install Go

Although VS Code can now read Go files, you still need to install the Go language itself on your computer to be able to read and execute Go code.

Visual Studio Code uses a search bar, called the Command Palette, to provide quick access to different commands. To open a new terminal inside of VS Code, open the Command Palette using ctrl+shift+P, or if on Mac OS Shift + Command + P, and then type View Terminal. You may also select View -> Terminal from the top left tabs. In the terminal, either enter:

For Windows / Linux:

sudo snap install go — classic

For Mac:

brew install go

Step 5: Hello World!

Now you are finally ready to start coding in Go on your very own personal computer!

1.Select Open Folder under the File tab in the upper left of Visual Studio Code. From here, select the directory of your Go installation. This should look like /home/`USER_NAME`/go.

2 .Re-open your terminal using the command palette the same way as in Step 4 and enter the command

mkdir -p ./src/hello_world

The -p flag stands for “parents”, and will automatically create the src directory for you if it does not already exist. In the future, try to create your projects under this /home/`USER_NAME`/go/src directory.

3. Use File -> Open Folder to open the new hello_world folder you created and use the cd src/hello_world command to change directories to your new project folder. If you have a hard time finding it in your file explorer, you can copy paste the result of pwd from your terminal into this section as well.

4. In the top left, right click the explorer tab and select create new file. Name this file hello_world.go. This is where we will store the actual Go code.

5. Copy paste this code into your new file:

package main
import "fmt"
func main() {
fmt.Println("hello world")
}

The package here being main indicates that the file contains a main function, which is used to call and execute code directly inside of the file. The import contains the fmt library that contains the functions Go uses to print words to your terminal, in this case “Hello World!”.

6. Execute

go mod init
go run .

inside of your terminal! Your go.mod file tracks your projects dependencies, and is a required prerequisite. Go run is used to execute Go code, and in this case the period indicates to run the command in your current directory. You can also use pwd to check if your terminal directory is correct.

If your code prints hello world to your terminal, then congratulations! You are ready to code in Go, aka Golang, from your very own computer! If not, then please feel free to leave feedback below or to contact me at https://www.linkedin.com/in/robertkeithrippetoe/. Happy coding!

--

--

Robert Keith Rippetoe
Robert Keith Rippetoe

Written by Robert Keith Rippetoe

Software Engineer with an emphasis on cloud infrastructure, devops, and site reliability.

No responses yet