Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Installing Go - Comprehensive Tutorial

Introduction

Go, often referred to as Golang, is a statically typed, compiled programming language designed at Google. Its syntax is clean and minimalistic, making it an excellent choice for both beginners and experienced developers. In this tutorial, we will guide you step-by-step through the process of installing Go on your system.

Step 1: Download the Go Installer

First, you need to download the Go installer from the official Go website. Navigate to the Go Downloads page and select the appropriate installer for your operating system (Windows, macOS, or Linux).

Example:

If you are using Windows, download the go1.xx.x.windows-amd64.msi file.

Step 2: Install Go

After downloading the installer, follow the instructions for your operating system:

Windows

  1. Run the go1.xx.x.windows-amd64.msi file.
  2. Follow the prompts of the installer.
  3. Once complete, Go will be installed in the directory C:\Go by default.

macOS

  1. Open the go1.xx.x.darwin-amd64.pkg file.
  2. Follow the prompts of the installer.
  3. Go will be installed in /usr/local/go by default.

Linux

  1. Extract the tarball using the following command:
  2. tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz
  3. Add Go to your PATH by adding these lines to your ~/.profile or ~/.bashrc file:
  4. export PATH=$PATH:/usr/local/go/bin

Step 3: Verify the Installation

To ensure that Go is installed correctly, open a terminal or command prompt and type the following command:

go version

You should see output similar to:

go version go1.xx.x linux/amd64

Step 4: Set Up Your Go Workspace

Go uses a workspace directory to organize your code. By default, Go expects your workspace to be located in $HOME/go on Unix systems, or %USERPROFILE%\go on Windows. Inside the workspace, you should have three directories:

  • src: Contains your Go source files.
  • pkg: Contains package objects.
  • bin: Contains executable commands.

Create these directories using the following commands:

mkdir -p $HOME/go/{src,pkg,bin}

Step 5: Write Your First Go Program

Create a directory for your first project inside the src directory. For example:

mkdir -p $HOME/go/src/hello

Inside this directory, create a file named hello.go with the following content:

package main

import "fmt"

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

Open a terminal, navigate to the hello directory, and run the following command to build and run your program:

go run hello.go

You should see the output:

Hello, World!

Conclusion

Congratulations! You have successfully installed Go and run your first Go program. This tutorial covered the basic steps to get you started with Go. From here, you can explore more advanced topics and start building your own Go applications.