Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Setting Up Go Environment

Introduction

Go, often referred to as Golang, is a statically typed, compiled programming language designed at Google. Before you can start coding in Go, you need to set up the Go environment on your machine. This tutorial will guide you through the steps required to set up the Go environment.

Step 1: Download Go

First, you need to download the Go installation package for your operating system from the official Go website.

Go to https://golang.org/dl/ and download the installer for your operating system.

Step 2: Install Go

Once the download is complete, follow the installation instructions for your operating system:

Windows

Run the installer and follow the prompts to complete the installation.

macOS

Open the downloaded package and follow the prompts to install Go. Alternatively, you can install Go using Homebrew:

brew install go

Linux

Extract the downloaded tarball to /usr/local directory:

tar -C /usr/local -xzf go1.x.x.linux-amd64.tar.gz

Then, add the Go binary to your PATH:

export PATH=$PATH:/usr/local/go/bin

Step 3: Set Up Go Workspace

Go uses a workspace to organize your Go code. Create a directory to serve as your workspace:

mkdir $HOME/go

Next, set the GOPATH environment variable to point to your workspace directory:

export GOPATH=$HOME/go

Step 4: Verify Installation

To verify that Go is installed correctly, open a terminal and run the following command:

go version
go version go1.x.x /

If you see the Go version information, then Go is installed correctly.

Step 5: Write Your First Go Program

Create a new directory for your Go project:

mkdir -p $GOPATH/src/hello

Navigate to the directory and create a file named hello.go:

cd $GOPATH/src/hello
nano hello.go

Add the following code to hello.go:

package main

import "fmt"

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

Save the file and exit the editor. Then, run your Go program using the following command:

go run hello.go
Hello, World!

If you see "Hello, World!" in the output, congratulations! You've successfully set up your Go environment and run your first Go program.