Skip to main content
These exercises will help you get comfortable with writing Go programs, understanding packages, and working with scopes.

Write Your First Program

These foundational exercises introduce you to the basics of Go syntax and program structure.
Objective: Learn cross-platform compilationBuild your Hello World program and share it with friends on different operating systems.Key Concepts:
  • Using go build
  • Cross-compilation with GOOS and GOARCH
  • Platform-specific executables
Approach:
  1. Build for your platform: go build
  2. For macOS: GOOS=darwin GOARCH=386 go build
  3. For Windows: GOOS=windows GOARCH=386 go build
  4. For Linux: GOOS=linux GOARCH=arm GOARM=7 go build
Note: On Windows Command Prompt or PowerShell, use:
cmd /c "set GOOS=darwin GOARCH=386 && go build"
Objective: Learn to print multiple values in one callCall Println or Print with multiple values separated by commas.Key Concepts:
  • Variadic functions
  • Multiple arguments
  • Space separation
Approach:
fmt.Println("Hello", "World", "from", "Go")
Objective: Understand Go’s syntax requirementsTry these experiments to learn what works and what doesn’t:
  1. Remove double quotes from a string literal
  2. Move package and import statements to the bottom of the file
Key Concepts:
  • String literal syntax
  • File structure requirements
  • Compiler error messages
Approach: Each experiment will produce an error - read the error messages carefully to understand Go’s requirements.

Packages and Scopes

Understanding packages and scopes is crucial for organizing your Go code effectively.
Objective: Learn to create and use custom packagesCreate multiple Go files with different packages and call their functions from main.Key Concepts:
  • Package declaration
  • Exported vs unexported identifiers
  • Importing local packages
  • File organization
Approach:
  1. Create a new directory for your package
  2. Declare package with package mypackage
  3. Create exported functions (capitalized names)
  4. Import and use in your main package
Objective: Learn how scoping works across packagesExperiment with variable and function visibility across different scopes.Key Concepts:
  • Package-level scope
  • Function-level scope
  • Block scope
  • Exported vs unexported identifiers
Approach:
  1. Create variables at different scope levels
  2. Try to access them from different locations
  3. Observe which identifiers are accessible where
  4. Remember: Capitalized = exported, lowercase = unexported
Objective: Handle package name conflictsImport the same package using different names (aliasing).Key Concepts:
  • Import aliases
  • Package naming conflicts
  • Custom import names
Approach:
import (
    f "fmt"
    fm "fmt"
)

func main() {
    f.Println("Using alias f")
    fm.Println("Using alias fm")
}

Additional Resources

Go Documentation

Explore the official Go package documentation to see what’s available in the standard library.

A Tour of Go

Take an interactive tour of Go’s features and syntax.

Next Steps

Once you’re comfortable with these basics, move on to:

Build docs developers (and LLMs) love