Step-by-Step Guide: Installing and Setting Up Go for Beginners

Shivam DubeyShivam Dubey
4 min read

Go (or Golang) is a modern programming language celebrated for its simplicity, speed, and robust support for concurrency. Whether you're developing web servers, command-line tools, or cloud services, Go provides an efficient and reliable platform. In this article, we’ll guide you through the steps to install and set up Go on your system, ensuring you're ready to start coding immediately.

Prerequisites

Before installing Go, ensure your system meets the following requirements:

  • Operating System: Go supports Windows, macOS, and Linux.

  • Internet Access: You’ll need an internet connection to download the installer.

  • Admin Rights: Administrator access may be required to install Go on your machine.

Note: The official Go website (https://go.dev) provides the latest version of Go. This guide assumes you are installing the most recent stable release.


Downloading and Installing Go

Step 1: Download the Go Installer

  1. Visit the official Go downloads page: https://go.dev/dl/.

  2. Select the appropriate installer for your operating system:

    • Windows: .msi installer

    • macOS: .pkg installer

    • Linux: .tar.gz archive

Step 2: Installing Go on Your System

Windows Installation

  1. Double-click the downloaded .msi file to start the installer.

  2. Follow the installation prompts and choose the default settings.

  3. By default, Go will be installed in C:\Program Files\Go.

macOS Installation

  1. Open the downloaded .pkg file.

  2. Follow the installer steps and use the default settings.

  3. Go will be installed in /usr/local/go.

Linux Installation

  1. Open the terminal and extract the .tar.gz archive using the following command:

     sudo tar -C /usr/local -xzf go1.x.x.linux-amd64.tar.gz
    
  2. This installs Go in /usr/local/go.

Tip: On Linux, you may need to manually update your PATH environment variable (explained in the next section).


Setting Up the Go Environment

To ensure Go runs correctly, you need to set up your environment variables, specifically the PATH and GOPATH.

Configuring PATH

The PATH environment variable tells your system where to find the Go executable (go). Follow the instructions based on your operating system:

Windows

  1. Open the Start menu, search for "Environment Variables," and click "Edit the system environment variables."

  2. In the System Properties window, click "Environment Variables."

  3. Find the Path variable, click "Edit," and add the Go binary path:

     C:\Program Files\Go\bin
    
  4. Click "OK" to save your changes.

macOS and Linux

  1. Open your terminal and edit your shell configuration file (~/.bashrc, ~/.zshrc, or ~/.profile):

     nano ~/.bashrc
    
  2. Add the following line to include Go in your PATH:

     export PATH=$PATH:/usr/local/go/bin
    
  3. Save the file and apply the changes:

     source ~/.bashrc
    

Setting Up GOPATH

The GOPATH is the directory where Go stores your source code, packages, and binaries. By default, Go uses $HOME/go as the GOPATH.

To manually set the GOPATH, add the following to your environment configuration file:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Verifying the Installation

To confirm that Go is installed correctly, open a terminal or command prompt and run:

go version

You should see output similar to:

go version go1.x.x linux/amd64

This indicates that Go has been installed successfully.

You can also check Go’s environment configuration using:

go env

This command displays all Go environment variables, including GOPATH and GOROOT.


Your First Go Program

Let’s write a simple "Hello, World!" program to test your Go setup.

  1. Open a terminal and create a new directory for your project:

     mkdir hello-go
     cd hello-go
    
  2. Create a new file named main.go with the following content:

     package main
    
     import "fmt"
    
     func main() {
         fmt.Println("Hello, World!")
     }
    
  3. Run the program using the go run command:

     go run main.go
    

    Output:

     Hello, World!
    

Congratulations! You’ve just written and executed your first Go program.


Using Go Modules

Go modules are the way Go manages dependencies and versions. Modules simplify the process of importing third-party packages and managing project dependencies.

Initializing a Go Module

  1. Inside your project directory, initialize a new Go module:

     go mod init hello-go
    

    This command creates a go.mod file, which keeps track of your module’s dependencies.

Adding a Dependency

To import a third-party package, simply use it in your code, and Go will automatically fetch it. For example:

package main

import (
    "fmt"
    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Hello())
}

Run the program:

go run main.go

Go will fetch the quote package automatically and print a quote.


Common Issues and Troubleshooting

  1. go: command not found Error

    • This error usually occurs when the Go binary is not in your PATH. Ensure you have added the correct path to your environment variables.
  2. Permission Issues on Linux/macOS

    • You might encounter permission errors during installation or when running commands. Use sudo or adjust the permissions of your Go installation directory.
  3. GOPATH Issues

    • If you face issues related to GOPATH, verify that the GOPATH environment variable is set correctly. The default location is $HOME/go.

Conclusion

Congratulations! You’ve successfully installed and set up Go on your system. You are now ready to start building powerful and efficient applications using Go. In future articles, we’ll explore more advanced topics, including Go’s concurrency model, web development, and integrating with databases.

Happy coding with Go!

0
Subscribe to my newsletter

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

Written by

Shivam Dubey
Shivam Dubey