Installing Go and Setting Up Your Development Environment

Tutorial 2 of 5

Introduction

This tutorial aims to guide you through installing Go, also known as Golang, and setting up your development environment. By the end of this tutorial, you will have a fully operational Go development environment on your computer.

What You Will Learn

  • How to Install Go.
  • How to Set Up Your Go Development Environment.
  • Basic Go Syntax and Concepts.

Prerequisites

  • A computer with an operating system such as Windows, macOS, or Linux.
  • Basic knowledge of how to use your computer's command line interface.
  • Some background in programming would be helpful but is not required.

Step-by-Step Guide

1. Installing Go

First, download the Go binary release suitable for your system from the official download page: https://golang.org/dl/.

After downloading, install it. The process will vary depending on your operating system:

  • Windows: Open the MSI file and follow the prompts to install Go. By default, Go will be installed in C:\Go, and its bin directory, C:\Go\bin, will be added to your PATH.

  • macOS: Open the package file and follow the prompts to install Go. The package installs the Go distribution to /usr/local/go.

  • Linux: Extract the downloaded archive and install to /usr/local:

    bash tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

Add /usr/local/go/bin to the PATH environment variable:

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

You can test the installation by opening a new terminal window and running:

go version

This should display the installed version of Go.

2. Setting Up Your Go Development Environment

After installing Go, create a workspace where you'll store your Go code. By convention, this is usually named go. Make a directory named go in your home directory:

mkdir ~/go

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

export GOPATH=$HOME/go

Then, add your workspace's bin subdirectory to your PATH:

export PATH=$PATH:$GOPATH/bin

Code Examples

Let's create a simple Go program:

  1. Inside your workspace, create a new directory src/hello:

    bash mkdir -p $GOPATH/src/hello

  2. Inside this directory, create a file named hello.go and add the following code:

    ```go
    package main

    import "fmt"

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

    • package main defines the package name of your program.
    • import "fmt" imports the fmt package which contains functions for formatting text, including printing to the console.
    • func main() is the main function where your program starts.
  3. You can run this program with go run:

    bash go run hello.go

    This should output: Hello, world!

Summary

You have now installed Go and set up your Go development environment. You've also written and run a simple Go program. Next steps could include exploring more of the Go standard library and learning about Go's type system and concurrency features.

Practice Exercises

  1. Modify the hello.go program to output Hello, YOURNAME, replacing YOURNAME with your actual name.

  2. Write a program that adds two numbers and outputs the result.

  3. Write a program that outputs the current date and time.

Remember, practice is key when learning a new programming language. Keep experimenting with new concepts and techniques.

Additional Resources