Signup/Sign In
PUBLISHED ON: MARCH 13, 2023

Introduction to Go and Visual Studio Code for Go Development

Go, also known as Golang, is a popular programming language developed by Google that is known for its simplicity, efficiency, and concurrency support. In order to start developing in Go, you will need to install the Go language and a code editor to write and edit your Go code. One popular code editor for Go is Visual Studio Code (VS Code), an open-source editor developed by Microsoft that supports a wide range of programming languages.

In this tutorial, we will show you how to install Go and VS Code on your system and set up a development environment for Go programming with VS Code. We will provide detailed instructions for installing Go and VS Code on MacOS, Windows, and Linux operating systems. With these tools installed and configured, you will be ready to start developing in Go.

How to install Golang?

To install Go, you need to follow the instructions for the specific operating system you use. Here is how you can install Go:

On MacOS

You can use Homebrew to install Go with the following command:

brew install go

On Windows

To install Go on Windows, follow these steps:

  1. Visit the Go downloads page and download the latest stable release of Go for Windows.
  2. Double-click the downloaded installer file to start the installation process.
  3. Wait for the installation to complete. Then, Click the "Finish" button to exit the installer.

After installing Go, you should set the GOROOT environment variable to point to the Go installation directory. You can do this by following these steps:

  1. Right-click on the "This PC" icon and select "Properties".
  2. In the left sidebar, click "Advanced system settings".
  3. In the "System Properties" window, click the "Environment Variables" button.
  4. In the "Environment Variables" window, scroll down to the "System variables" section and click the "New" button.
  5. In the "New System Variable" window, enter "GOROOT" as the variable name and the path to the Go installation directory as the variable value. For example, C:\Go.
  6. Click the "OK" button to close the window.

You should also set the GOPATH environment variable to point to your Go workspace. This is the directory where your Go code and libraries will be stored. You can set the GOPATH variable by following these steps:

  1. In the "Environment Variables" window, scroll down to the "System variables" section and click the "New" button.
  2. In the "New System Variable" window, enter "GOPATH" as the variable name and the path to your Go workspace as the variable value. For example, C:\go\src.
  3. Click the "OK" button to close the window.

You should also add the Go bin directory to your PATH variable so that you can use Go tools and executables from the command line. To do this, follow these steps:

  1. In the "Environment Variables" window, scroll down to the "System variables" section and locate the "Path" variable.
  2. Click the "Edit" button.
  3. In the "Edit Environment Variable" window, click the "New" button and enter the path to the Go bin directory. For example, C:\Go\bin.
  4. Click the "OK" button to close the window.

On Linux

You can use your package manager to install Go. For example, on Debian or Ubuntu, you can use the following command:

sudo apt-get install golang

After installing Go, you should set the GOPATH environment variable to point to your Go workspace. This is the directory where your Go code and libraries will be stored. You can set the GOPATH variable in your .bashrc or .bash_profile file like this:

export GOPATH=$HOME/go

You should also add the Go bin directory to your PATH variable so that you can use Go tools and executables from the command line. You can do this by adding the following line to your .bashrc or .bash_profile file:

export PATH=$PATH:$GOPATH/bin

Installing Visual Studio Code

Follow the instructions to install VS Code or visit the (VS Code download page):

on Windows

To install VS Code on Windows, follow these steps:

  1. Visit the VS Code download page and click the "Download" button for Windows.
  2. Double-click the downloaded installer file to start the installation process.
  3. Wait for the installation to complete. Click the "Finish" button to exit the installer.

on Linux

To install VS Code on Linux, follow these steps:

  1. Visit the VS Code download page and click the "Download" button for Linux.
  2. Open a terminal and navigate to the directory where the downloaded package is located.
  3. Extract the package using the tar command:
tar -xvf code_*.tar.gz
  1. Navigate to the extracted directory and run the install.sh script:
cd code-* ./install.sh
  1. Follow the prompts to complete the installation.

on MacOS

To install VS Code on macOS, follow these steps:

  1. Visit the VS Code download page and click the "Download" button for macOS.
  2. Double-click the downloaded package to start the installation process.
  3. Drag the "Visual Studio Code" icon to the "Applications" folder.
  4. Double-click the "Visual Studio Code" icon to launch the application.

Setting Up Go Development with VS Code

Once you have installed Go and VS Code, you can set up your Go development environment by installing the Go extension for VS Code. To do this, follow these steps:

  1. Open VS Code and click the extensions icon in the left sidebar.
  2. In the search box, enter "Go" and press Enter.
  3. Click the Install button for the Go extension.
  4. After the extension is installed, click the Reload button to activate it.

With the Go extension installed, you can now use VS Code to write and run Go code. To create a new Go project, follow these steps:

  1. Create a new directory for your project and navigate to it in the terminal.
  2. Run the go mod init command to create a new Go module.
  3. Create a new file in VS Code and save it with a .go extension.
  4. Start writing Go code in the file.

To run your Go code, you can use the VS Code integrated terminal or the Go Playground. To open the integrated terminal, click the terminal icon in the bottom toolbar or use the Ctrl+Shift+` shortcut. To open the Go Playground, click the "Go to Playground" button in the status bar.

Conclusion

In this tutorial, we provided detailed instructions on how to install Go and VS Code on your system and set up a development environment for Go programming with VS Code. These instructions include downloading the necessary installation files, setting environment variables, and adding necessary directories to the PATH variable. By following these steps, you can easily install Go and VS Code and begin developing in Go using the VS Code editor.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.