Setting Up the Go Environment

Setting up the Go environment on your computer is the first step to start programming in Go. In this chapter, we will follow simple steps to get Go installed and ready to use.

Step 1: Download Go

  1. Go to the official Go website.
  2. Choose the download link for your operating system (Windows, macOS, or Linux).
  3. Download the installer file.

Step 2: Install Go

On Windows

  1. Open the downloaded .msi file.
  2. Follow the installer instructions. It will install Go to C:\Go by default.
  3. After the installation is complete, the Go binaries will be added to your PATH environment variable.

On macOS

  1. Open the downloaded .pkg file.
  2. Follow the installer instructions. It will install Go to /usr/local/go by default.
  3. Ensure that the Go binaries are in your PATH. You may need to add export PATH=$PATH:/usr/local/go/bin to your shell profile (e.g., .bash_profile, .zshrc).

On Linux

  1. Open a terminal.
  2. Navigate to the directory where you downloaded the Go tarball.
  3. Extract the tarball using the following command:
    tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz
    

    Replace go1.xx.x.linux-amd64.tar.gz with the name of the downloaded file.

  4. Add Go to your PATH by adding the following line to your shell profile (e.g., .bashrc, .zshrc):
    export PATH=$PATH:/usr/local/go/bin
    
  5. Apply the changes by running:
    source ~/.bashrc
    

    or

    source ~/.zshrc
    

Step 3: Verify the Installation

  1. Open a terminal or command prompt.
  2. Type the following command:
    go version
    
  3. You should see output similar to:
    go version go1.xx.x <OS/ARCH>
    

    This confirms that Go is installed correctly.

Step 4: Set Up the Workspace

  1. Create a directory for your Go workspace. This is where your Go projects will be stored.
    mkdir -p $HOME/go/{bin,src,pkg}
    

    This creates the bin, src, and pkg directories inside your go directory.

  2. Set the GOPATH environment variable to point to your workspace. Add the following line to your shell profile:
    export GOPATH=$HOME/go
    
  3. Add the bin directory to your PATH. Add the following line to your shell profile:
    export PATH=$PATH:$GOPATH/bin
    
  4. Apply the changes by running:
    source ~/.bashrc
    

    or

    source ~/.zshrc
    

Conclusion

You have now successfully set up the Go environment on your computer. You are ready to start developing applications using Go. In the next post, we will create your first Go program.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top