Go Installation and Setup - Getting Started with Go Programming

Learning Objectives: By the end of this lesson, you will be able to:

  • Install Go on your operating system (Windows, macOS, Linux)
  • Set up your Go development environment with proper GOPATH and GOROOT
  • Understand Go modules and dependency management
  • Use essential Go CLI commands effectively
  • Create and run your first Go program
  • Set up your IDE/editor for Go development

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Have you ever wondered why companies like Google, Docker, Kubernetes, and Dropbox chose Go for their critical systems? What makes a programming language so attractive for building high-performance, concurrent applications that can handle millions of requests?

Lesson Overview

  • Duration: 4-6 hours
  • Prerequisites: Basic understanding of programming concepts, familiarity with command line/terminal
  • Materials Needed: Computer with internet access, terminal/command prompt access

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. Go Overview and Benefits - Understanding what makes Go special
  2. Installation Process - Step-by-step installation for all major platforms
  3. Environment Configuration - Setting up GOPATH, GOROOT, and workspace
  4. Go Modules - Modern dependency management approach
  5. Essential CLI Commands - Tools you’ll use daily
  6. First Go Program - Hello World and beyond
  7. IDE Setup - Configuring your development environment

Success Criteria:

By the end of this session, you should be able to install Go, configure your environment, create a new Go module, and run Go programs from the command line.

3. Foundation Knowledge (15-20 minutes)

What is Go?

Go (also known as Golang) is an open-source programming language developed at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It was designed to address the challenges of large-scale software development at Google.

Real-World Analogy: Think of Go as the Swiss Army knife of programming languages - it’s simple, reliable, and efficient. Just like a Swiss Army knife combines multiple tools in one compact design, Go combines the simplicity of Python, the performance of C, and built-in concurrency support all in one language.

Why This Matters: Go solves real problems that developers face daily:

  • Fast compilation: No more waiting minutes for your code to compile
  • Built-in concurrency: Handle thousands of concurrent operations easily
  • Simple syntax: Less cognitive overhead, more focus on solving problems
  • Strong standard library: Most common tasks covered out of the box
  • Cross-platform: Write once, compile for any platform

Core Concepts

Go’s Design Philosophy

Go follows three main principles:

  1. Simplicity: Fewer language features mean less to learn and fewer ways to make mistakes
  2. Readability: Code should be easy to read and understand
  3. Efficiency: Both in development time and runtime performance

Key Features

  • Static typing with type inference
  • Garbage collection for automatic memory management
  • Built-in concurrency with goroutines and channels
  • Fast compilation times
  • Cross-platform compilation
  • Rich standard library

Key Terminology

  • GOROOT: The directory where Go is installed
  • GOPATH: Your workspace directory (legacy approach)
  • Go Modules: Modern dependency management system
  • Package: A collection of Go source files in the same directory
  • Module: A collection of packages with versioning support
  • Goroutine: Lightweight thread managed by Go runtime
  • Channel: Communication mechanism between goroutines

4. Detailed Explanation and Examples (25-30 minutes)

Understanding Go’s Environment Model

Go has evolved its workspace model over time. Initially, Go used the GOPATH model where all Go code lived in a single workspace. Modern Go (1.11+) uses Go Modules, which allows you to work on Go projects anywhere on your system.

GOPATH vs Go Modules

GOPATH (Legacy):

  • Required all Go code to be in a specific directory structure
  • Single workspace for all projects
  • Difficult to manage different versions of dependencies

Go Modules (Modern):

  • Projects can live anywhere on your system
  • Each project manages its own dependencies
  • Semantic versioning support
  • Easier collaboration and deployment

Practical Applications

Go modules are used in:

  • Microservices: Each service can have its own dependencies
  • Enterprise applications: Better dependency isolation
  • Open source projects: Easier contribution and distribution
  • CI/CD pipelines: Reproducible builds

Common Misconceptions

  1. Misconception: You still need to set up GOPATH for modern Go development Reality: Go modules eliminate the need for GOPATH in most cases

  2. Misconception: Go is only good for systems programming Reality: Go excels in web development, APIs, CLI tools, and cloud applications

5. Live Coding Demonstration (20-25 minutes)

Example 1: Installing Go

For macOS (using Homebrew):

# Install Homebrew if you haven't already
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Go
brew install go

# Verify installation
go version

For Ubuntu/Debian Linux:

# Download Go (check for latest version at https://golang.org/dl/)
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz

# Remove any previous Go installation and extract
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

# Add Go to PATH in your shell profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc

# Verify installation
go version

For Windows:

# Download installer from https://golang.org/dl/
# Run the MSI installer
# The installer will automatically set up PATH

# Verify in Command Prompt or PowerShell
go version

Explanation:

  • The go version command confirms Go is properly installed and shows the version
  • The installer automatically sets up the necessary environment variables
  • Go’s binary will be available system-wide after proper installation

Example 2: Setting Up Your First Module

# Create a new directory for your project
mkdir hello-go
cd hello-go

# Initialize a new Go module
go mod init hello-go

# Create main.go file
touch main.go

Key Points:

  • go mod init creates a new module with a go.mod file
  • The module name can be anything, but use domain-style names for public modules
  • This creates an isolated workspace for your project

Example 3: Your First Go Program

// main.go
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Hello, Go World!")
    fmt.Printf("Current time: %s\n", time.Now().Format("2006-01-02 15:04:05"))
    
    // Demonstrate Go's simplicity
    name := "Gopher"
    fmt.Printf("Welcome to Go programming, %s!\n", name)
}

Key Points:

  • package main declares this as an executable program
  • import brings in standard library packages
  • func main() is the entry point of the program
  • := is short variable declaration syntax

Running Your Program

# Run directly
go run main.go

# Build and run
go build
./hello-go  # On Linux/macOS
# or
hello-go.exe  # On Windows

# Build for different platforms
GOOS=linux GOARCH=amd64 go build -o hello-linux
GOOS=windows GOARCH=amd64 go build -o hello-windows.exe
GOOS=darwin GOARCH=amd64 go build -o hello-macos

Debugging Demonstration

Show common setup errors and how to fix them:

Problem: “go: command not found”

# Check if Go is in PATH
echo $PATH

# If Go is missing, add to your shell profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc

Problem: Module import issues

# If you see "package not found" errors
go mod tidy  # Downloads and cleans up dependencies
go mod download  # Downloads dependencies without building

6. Hands-On Practice (30-40 minutes)

Exercise 1: Environment Verification (10 minutes)

Objective: Verify your Go installation and understand the environment

Instructions:

  1. Check your Go version and installation details
  2. Verify your GOROOT and GOPATH settings
  3. Create a simple program to display environment information

Starter Code:

package main

import (
    "fmt"
    "os"
    "runtime"
)

func main() {
    // TODO: Display Go version
    // TODO: Display GOROOT
    // TODO: Display current working directory
    // TODO: Display operating system and architecture
}

Solution:

package main

import (
    "fmt"
    "os"
    "runtime"
)

func main() {
    fmt.Println("=== Go Environment Information ===")
    fmt.Printf("Go Version: %s\n", runtime.Version())
    fmt.Printf("GOROOT: %s\n", runtime.GOROOT())
    fmt.Printf("Operating System: %s\n", runtime.GOOS)
    fmt.Printf("Architecture: %s\n", runtime.GOARCH)
    
    pwd, _ := os.Getwd()
    fmt.Printf("Current Directory: %s\n", pwd)
    
    fmt.Printf("GOPATH: %s\n", os.Getenv("GOPATH"))
}

Exercise 2: Module Creation and Management (15 minutes)

Objective: Practice creating modules and managing dependencies

Requirements:

  • Create a new module called “calculator”
  • Add a dependency on a popular Go package
  • Create a simple calculator with basic operations
  • Use proper Go module commands

Instructions:

  1. Create a new directory and initialize a Go module
  2. Add a dependency (e.g., github.com/fatih/color for colored output)
  3. Implement basic arithmetic operations
  4. Build and run your program

Hints:

  • Use go get to add dependencies
  • Check your go.mod file after adding dependencies
  • Use go mod tidy to clean up unused dependencies

Exercise 3: Cross-Platform Building (15 minutes)

Objective: Understand Go’s cross-compilation capabilities

Challenge: Create a simple system information tool that can be built for multiple platforms

Requirements:

  • Display system information (OS, architecture, Go version)
  • Build the program for at least 3 different platforms
  • Test the executable on your current platform

Extension Ideas:

  • Add command-line flags using the flag package
  • Include build time and version information
  • Create a simple shell script to build for multiple platforms

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Mixing GOPATH and Go modules Cause: Using old tutorials that reference GOPATH with modern Go Solution: Use Go modules exclusively for new projects; ignore GOPATH setup

  2. Problem: Module name errors in imports Cause: Using incorrect module names or paths Solution: Ensure module name in go.mod matches import paths

  3. Problem: Environment variables not persisting Cause: Not updating shell profile or not sourcing changes Solution: Add environment variables to ~/.bashrc, ~/.zshrc, or system settings

Debugging Tips

  • Use go env to check all Go environment variables
  • Use go mod why <package> to understand why a dependency is needed
  • Use go mod graph to visualize dependency relationships
  • Check go.mod and go.sum files for dependency information

Installation Issues by Platform

macOS

  • Issue: Permission denied errors
  • Solution: Use Homebrew or official installer instead of manual installation
  • Issue: Multiple Go versions
  • Solution: Use brew unlink go && brew link go to ensure correct version

Linux

  • Issue: PATH not updated
  • Solution: Add Go binary path to shell profile and source it
  • Issue: Permission issues with /usr/local
  • Solution: Use sudo for installation or install to user directory

Windows

  • Issue: PATH not recognized in new terminals
  • Solution: Restart terminal or use Windows environment variable GUI
  • Issue: Antivirus blocking Go binaries
  • Solution: Add Go installation directory to antivirus exceptions

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Module Naming: Use domain-style names for public modules (e.g., github.com/username/project)
  • Version Management: Use semantic versioning for your modules
  • Project Structure: Follow Go’s conventional project layout
  • Environment Management: Use Go modules instead of GOPATH for all new projects

Development Environment Setup

  • VS Code: Install the Go extension by Google
  • GoLand: JetBrains IDE with excellent Go support
  • Vim/Neovim: Use vim-go plugin for terminal-based development
  • Essential tools: Install gofmt, golint, go vet for code quality

Next Steps in Your Learning Journey

  • Go Tour: Complete the interactive Go tour at tour.golang.org
  • Effective Go: Read the official style guide
  • Standard Library: Explore Go’s extensive standard library
  • Community: Join Go communities and forums for support

9. Summary and Review (5 minutes)

Key Takeaways

  1. Go Installation: Go provides simple installation methods for all major platforms
  2. Modern Workflow: Use Go modules for dependency management and project organization
  3. Development Tools: Go comes with powerful CLI tools for building, testing, and managing code
  4. Cross-platform: Go makes it easy to build applications for different operating systems

Quick Review Questions

  1. What command would you use to create a new Go module in an empty directory?
  2. How do you add a new dependency to your Go project?
  3. What’s the difference between go run and go build?
  4. How do you build a Go program for a different operating system?

Answers:

  1. go mod init <module-name>
  2. go get <package-name> or import it and run go mod tidy
  3. go run compiles and runs immediately; go build creates an executable file
  4. Set GOOS and GOARCH environment variables before building

For Instructors

Teaching Tips

  • Demo Live: Always demonstrate installation on the same OS as your students
  • Troubleshooting Time: Allow extra time for installation issues - they’re common
  • Version Awareness: Be aware of Go version differences and compatibility
  • Environment Emphasis: Stress the importance of proper environment setup early

Assessment Ideas

  • Installation Check: Have students run go version and go env
  • Module Creation: Students create and build a simple module
  • Cross-compilation: Build the same program for different platforms
  • Troubleshooting: Present common errors and have students solve them

Adaptation Notes

  • Beginner Focus: Spend more time on environment concepts and troubleshooting
  • Advanced Students: Focus on build optimizations and advanced CLI options
  • Online Delivery: Use screen sharing for installation demos
  • Time Management: Installation can take longer than expected - have backup activities

Common Student Questions

  1. “Do I need to use GOPATH?” - Not with Go modules (Go 1.11+)
  2. “Why does my PATH not work?” - Usually need to restart terminal or source profile
  3. “Can I use Go without modules?” - Possible but not recommended for new projects
  4. “What IDE should I use?” - VS Code with Go extension is great for beginners