Day 1: Setting Up Your Go Development Environment on Windows, macOS, and Linux

Venkat Annangi
Venkat Annangi
23/09/2024 15:30 2 min read 49 views
#golang #108 days of golang

Part of Series: 108 days of Golang

Article 1 of 22

Day 1: Setting Up Your Go Development Environment on Windows, macOS, and Linux

To begin our journey, we’ll set up Go on different operating systems (Windows, macOS, and Linux). We’ll go through the installation process step-by-step, configure our environment, and write a simple "Hello, World!" program to test the setup.

Step 1: Installing Go

Let’s walk through the installation process for each platform:

Windows

  • Go to the official Go Downloads page and download the Windows installer.
  • Run the installer and follow the setup instructions.
  • Once installed, open Command Prompt and run go version to verify the installation.

macOS

  • If you use Homebrew, installing Go is simple:

    brew install go
  • If not, download the Go package from the Go Downloads page, run the installer, and follow the instructions.
  • Verify the installation by running go version in your terminal.

Linux

  • Download the tarball for your distribution from the Go Downloads page.
  • Extract the files and move the contents to /usr/local:
tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz
  • Set up your environment variables (add these to your ~/.profile or ~/.bashrc):
export PATH=$PATH:/usr/local/go/bin
  • Verify by running go version.

Step 2: Setting Up Environment Variables

After installation, ensure Go is correctly set up by configuring the necessary environment variables:

export GOROOT=/usr/local/go  
export GOPATH=$HOME/go  
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

GOROOT points to the location where Go is installed. GOPATH defines your workspace where your Go projects will live. Finally, we add Go binaries to the system PATH.

Step 3: Verify the Installation

Open your terminal (or Command Prompt on Windows) and run:

go version

If you see something like go version go1.XX.X, then you’ve successfully installed Go.

Step 4: Writing Your First Go Program

Create a new directory for your Go code:

mkdir hello_go

Navigate into the directory:

cd hello_go

Create a file called main.go and write your first Go program:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Save the file and run the following command to execute your Go program:

go run main.go

That's it! You’ve successfully set up Go and written your first Go program.

Comments