Module: Introduction to Go

Installing Go

Installing Go

This section details how to install Go on various operating systems.

1. Downloading Go

First, you need to download the appropriate Go distribution for your operating system. You can find the latest releases at the official Go downloads page:

https://go.dev/dl/

Choose the package that corresponds to your OS and architecture. Common options include:

  • Windows: go<version>.windows-amd64.msi (for 64-bit Windows) or go<version>.windows-386.msi (for 32-bit Windows)
  • macOS: go<version>.darwin-amd64.pkg (for Intel Macs) or go<version>.darwin-arm64.pkg (for Apple Silicon Macs)
  • Linux: go<version>.linux-amd64.tar.gz (for 64-bit Linux), go<version>.linux-386.tar.gz (for 32-bit Linux), go<version>.linux-arm64.tar.gz (for ARM64 Linux)

Replace <version> with the latest version number.

2. Installation Instructions

The installation process varies slightly depending on your operating system.

a) Windows

  1. Run the MSI installer: Double-click the downloaded .msi file.

  2. Follow the prompts: Accept the license agreement and choose an installation location. The default location is usually C:\Program Files\Go.

  3. Environment Variables: The installer should automatically configure the necessary environment variables. However, it's good to verify:

    • GOROOT: This should point to your Go installation directory (e.g., C:\Program Files\Go).
    • GOPATH: This is your workspace directory where your Go projects will reside. You can choose any location, but a common choice is %USERPROFILE%\go.
    • PATH: The bin directory within your GOROOT (e.g., C:\Program Files\Go\bin) should be added to your PATH environment variable. This allows you to run go commands from any command prompt.

    To verify/set environment variables:

    • Search for "Environment Variables" in the Windows search bar.
    • Click "Edit the system environment variables".
    • Click "Environment Variables...".
    • Under "System variables", check for GOROOT, GOPATH, and edit Path to include the Go bin directory.

b) macOS

  1. Run the PKG installer: Double-click the downloaded .pkg file.

  2. Follow the prompts: Accept the license agreement and choose an installation location. The default location is usually /usr/local/go.

  3. Environment Variables: You need to manually configure the environment variables. Add the following lines to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

    export GOROOT=/usr/local/go
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    • GOROOT: Points to the Go installation directory.
    • GOPATH: Your workspace directory.
    • PATH: Adds the Go bin directory to your path.
  4. Reload your shell: After modifying your shell configuration file, reload it by running source ~/.bashrc or source ~/.zshrc (depending on your shell).

c) Linux

  1. Extract the archive: Open a terminal and navigate to the directory where you downloaded the tar.gz file. Then, extract the archive using the following command:

    tar -C /usr/local -xzf go<version>.linux-amd64.tar.gz  # Replace with your filename
    

    This extracts the Go files to /usr/local/go. You can choose a different location if you prefer.

  2. Environment Variables: Add the following lines to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

    export GOROOT=/usr/local/go
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    • GOROOT: Points to the Go installation directory.
    • GOPATH: Your workspace directory.
    • PATH: Adds the Go bin directory to your path.
  3. Reload your shell: After modifying your shell configuration file, reload it by running source ~/.bashrc or source ~/.zshrc (depending on your shell).

3. Verifying the Installation

After installation, verify that Go is installed correctly by opening a new terminal or command prompt and running the following command:

go version

This should print the Go version that you installed. For example:

go version go1.21.5 darwin/amd64

If you see this output, Go is installed successfully!

4. Understanding GOPATH and Modules (Go 1.11+)

  • GOPATH: Historically, GOPATH was crucial for managing Go projects. It defined the location of your source code, packages, and binaries. However, with the introduction of Go Modules (in Go 1.11), GOPATH is less critical.

  • Go Modules: Go Modules are the recommended way to manage dependencies in modern Go projects. They allow you to specify dependencies directly in your project's go.mod file, without relying on GOPATH. You can start a module with go mod init <module_name>.

While GOPATH is still used, especially for older projects, it's best to learn and use Go Modules for new projects. You can even use modules inside your GOPATH.

This completes the installation process. You are now ready to start writing and running Go programs!