Getting Started with Golang: Your First Steps

Shivam JhaShivam Jha
4 min read

Hey there, aspiring Go programmer! If you're ready to dive into the world of Golang but aren’t sure where to start, you’re in the right place. This blog will guide you through the essentials: installing Go, setting up your environment, and picking an IDE or text editor. Let’s get your Go journey off to a smooth start!

Installing Go: A Step-by-Step Guide

Before you can start writing Go code, you need to install it on your machine. Don’t worry—it’s a straightforward process, and I’m here to walk you through it for Windows, macOS, and Linux.

1. Installing Go on Windows

  1. Download the Installer:

  2. Run the Installer:

    • Double-click the downloaded file to start the installation. Follow the prompts, and make sure to keep the default settings unless you have a specific reason to change them.
  3. Verify Installation:

    • Open Command Prompt and type go version. If everything is set up correctly, you should see the Go version you installed.

2. Installing Go on macOS

  1. Download the Installer:

  2. Run the Installer:

    • Double-click the .pkg file to begin the installation. Follow the instructions, and the Go binaries will be installed in /usr/local/go.
  3. Verify Installation:

    • Open Terminal and type go version. You should see the Go version number displayed.

3. Installing Go on Linux

  1. Download the Tarball:

    • Go to the Go downloads page and download the appropriate tarball for Linux (.tar.gz file).
  2. Extract the Tarball:

    • Open Terminal and navigate to the directory where you downloaded the file. Run:

        tar -C /usr/local -xzf go1.xx.linux-amd64.tar.gz
      
    • Replace go1.xx.linux-amd64.tar.gz with the filename you downloaded.

  3. Set Up Your Path:

    • Add Go to your PATH by editing the .bashrc or .zshrc file in your home directory:

        export PATH=$PATH:/usr/local/go/bin
      
    • Reload your shell configuration with source ~/.bashrc or source ~/.zshrc.

  4. Verify Installation:

    • In Terminal, type go version to check that Go is correctly installed.

Setting Up GOPATH and GOROOT Environment Variables

With Go installed, you need to set up a couple of environment variables: GOPATH and GOROOT.

  • GOROOT: This is where Go is installed. Usually, you don’t need to set this manually if you use the default installation paths, but if needed, it should point to the Go installation directory (e.g., /usr/local/go).

  • GOPATH: This is where your Go workspace resides. It contains your Go projects and third-party packages. By default, this is set to your home directory’s go folder (e.g., ~/go).

To set these variables:

  1. Windows:

    • Open System Properties > Advanced > Environment Variables.

    • Add a new system variable named GOPATH and set it to your workspace directory (e.g., C:\Users\YourName\go).

    • If necessary, add GOROOT pointing to your Go installation directory.

  2. macOS/Linux:

    • Edit your shell configuration file (.bashrc, .zshrc, etc.) and add:

        export GOROOT=/usr/local/go
        export GOPATH=$HOME/go
        export PATH=$PATH:$GOROOT/bin
      
    • Apply the changes with source ~/.bashrc or source ~/.zshrc.

Installing a Go IDE or Text Editor

A good IDE or text editor can make coding in Go a breeze. Here are two popular choices:

1. Visual Studio Code (VSCode)

VSCode is a versatile editor with robust support for Go. To set it up:

  1. Install VSCode:

  2. Install Go Extension:

    • Open VSCode, go to the Extensions view (Ctrl+Shift+X), and search for “Go”. Install the official Go extension by the Go team.
  3. Configure Go:

    • Open a Go file, and the extension will prompt you to install additional Go tools. Follow the prompts to complete the setup.

2. GoLand

GoLand is a powerful IDE specifically designed for Go development by JetBrains. It’s a great choice if you’re looking for advanced features and support.

  1. Install GoLand:

  2. Set Up GoLand:

    • Follow the installation instructions and configure GoLand to use your Go SDK. You might need to specify your GOROOT and GOPATH during the setup process.

Wrapping It Up

And there you have it—your comprehensive guide to getting started with Golang! From installing Go on various platforms to setting up your environment and choosing the right tools, you’re all set to begin your Go programming adventure. So grab your favorite beverage, fire up your new Go environment, and start coding. Welcome to the world of Go, where efficiency and simplicity reign supreme! 🚀

Ready to dive in? The Go community is waiting to welcome you with open arms. Happy coding 🐳

0
Subscribe to my newsletter

Read articles from Shivam Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shivam Jha
Shivam Jha

LFX'24 @Kyverno | Web Dev | DevOps | OpenSource | Exploring Cloud Native Technologies.