Getting Started With Go

Overview

This course will walk you through installing Go and creating your first Go module. Modern Go development uses Go modules for dependency management, which is the default since Go 1.16. You’ll learn to verify your installation and choose a suitable code editor for Go development.

Installing GO

Preferred Method

Download the latest installer from go: http://golang.org/dl/

Mac - Using Home Brew

It’s preferred that you download and install the package. However, if you are a homebrew user, you can use the following commands:

$ brew update
$ brew install go

Linux - Using APT

$ sudo apt-get update
$ sudo apt-get install golang

Note: This can leave you with an old version of go as they don’t update the package manager as timely as they should.

Verify Installation

After installing Go, verify the installation by checking the version:

$ go version
go version go1.25.0 darwin/amd64

You should see the version number and your operating system/architecture.

Go Modules

Go modules are the standard way to manage dependencies in Go. Since Go 1.16, modules are enabled by default.

Creating Your First Module

Create a new directory for your project anywhere on your system:

$ mkdir myproject
$ cd myproject
$ go mod init myproject

This creates a go.mod file that tracks your module name and dependencies.

Module Structure

A typical Go module looks like this:

myproject/
├── go.mod          # Module definition and dependencies
├── go.sum          # Checksums for dependencies
├── main.go         # Main application entry point
└── internal/       # Private packages
    └── util/
        └── helper.go

You can create Go projects anywhere on your filesystem - there’s no required location.

Working With Modules

Adding Dependencies

Dependencies are automatically added when you import and build:

$ go build    # Downloads and adds dependencies
$ go mod tidy # Cleans up unused dependencies

Installing Go Tools

Use go install to install command-line tools:

$ go install golang.org/x/tools/gopls@latest

Tools are installed to $GOPATH/bin (defaults to $HOME/go/bin).

Add Go Bin To `$PATH`

To run installed Go tools, add the Go bin directory to your PATH:

Linux/macOS - Add to .bashrc, .zshrc, or equivalent:

export PATH=$PATH:$(go env GOPATH)/bin

Windows - Add %USERPROFILE%\go\bin to your PATH environment variable.

Historical Context

The `$GOPATH` (Legacy)

Prior to Go modules (before Go 1.11), all Go code had to live in a single workspace called $GOPATH. This is no longer required.

When you might still encounter GOPATH:

  • Working with very old codebases
  • Some legacy tools expect it to be set

The $GOPATH defaults to $HOME/go if not explicitly set. The $GOPATH/bin directory is still used for installed tools.

Note: $GOROOT is the Go installation path (e.g., /usr/local/go), which is different from $GOPATH.

Exercise: System Check


package main

import (
	"fmt"
	"os/exec"
	"runtime"
	"strings"
)

func main() {
	fmt.Println("Checking Go environment...")
	fmt.Println()

	// Check Go version
	cmd := exec.Command("go", "version")
	output, err := cmd.Output()
	if err != nil {
		fmt.Println("❌ Go is not installed or not in PATH")
		fmt.Println("   Please install Go from https://go.dev/dl/")
		return
	}
	fmt.Printf("✓ Go version: %s", output)

	// Display runtime information
	fmt.Printf("✓ OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)

	// Check if go modules work
	cmd = exec.Command("go", "env", "GOVERSION")
	output, err = cmd.Output()
	if err != nil {
		fmt.Println("❌ Unable to query Go environment")
		return
	}
	version := strings.TrimSpace(string(output))
	fmt.Printf("✓ GOVERSION: %s\n", version)

	// Check GOPATH/bin for installed tools
	cmd = exec.Command("go", "env", "GOPATH")
	output, err = cmd.Output()
	if err == nil {
		gopath := strings.TrimSpace(string(output))
		if gopath != "" {
			fmt.Printf("✓ GOPATH: %s\n", gopath)
			fmt.Printf("  (Tools installed via 'go install' go to %s/bin)\n", gopath)
		}
	}

	fmt.Println()
	fmt.Println("✓ Success! Your Go environment is ready.")
}
$ go run env_check.go

This verifies that Go is properly installed and your environment is set up correctly.

Additional Setup

Version Control

Git is essential for Go development. Most Go packages are hosted on GitHub and other Git-based platforms.

Install Git:

  • macOS: brew install git or Xcode Command Line Tools
  • Linux: sudo apt-get install git or equivalent for your distribution
  • Windows: git-scm.com/download/win

Verify installation:

$ git --version
git version 2.40.0

Editors

The most popular editor for Go development with excellent tooling support.

GoLand

Full-featured Go IDE from JetBrains with built-in Go support.

Vim / Neovim

Popular for developers who prefer terminal-based editing.

Zed

A modern, high-performance editor with built-in Go support.

Sublime Text

Emacs

More Editors

For a full list of editors and IDE plugins, see the Go wiki