Introduction to Go

What is Go?

Go (often called Golang) is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It was released in 2009 with the following goals:

  • Simplicity: Easy to learn and read
  • Efficiency: Fast compilation and execution
  • Concurrency: Built-in support for concurrent programming
  • Reliability: Strong typing and garbage collection

Why Go?

Strengths

  1. Fast Compilation: Go compiles directly to machine code, and compilation is extremely fast
  2. Simple Syntax: Minimal keywords, no classes, no inheritance complexity
  3. Built-in Concurrency: Goroutines and channels make concurrent programming natural
  4. Standard Library: Rich stdlib for networking, HTTP, JSON, testing, etc.
  5. Single Binary: Compiles to a single static binary with no dependencies
  6. Cross-Platform: Easy cross-compilation for different OS/architectures
  7. Tooling: Excellent built-in tools (formatter, linter, test runner, etc.)

Common Use Cases

  • Web services and APIs
  • CLI tools
  • DevOps and infrastructure (Docker, Kubernetes are written in Go)
  • Microservices
  • Network programming
  • Cloud-native applications

Installation

macOS

# Using Homebrew
brew install go

# Or download from https://go.dev/dl/

Linux

# Ubuntu/Debian
sudo apt update
sudo apt install golang-go

# Or download the tarball from https://go.dev/dl/
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

Windows

Download the MSI installer from https://go.dev/dl/ and run it.

Verify Installation

go version
# Output: go version go1.21.5 linux/amd64 (or similar)

Environment Setup

GOPATH and GOROOT

  • GOROOT: Where Go is installed (usually automatic)
  • GOPATH: Your workspace (default: $HOME/go)
# Check your Go environment
go env

# Key variables
go env GOROOT  # Go installation path
go env GOPATH  # Your workspace path
go env GOBIN   # Where binaries are installed

Project Structure (Modern Go with Modules)

Since Go 1.11+, Go modules are the standard for dependency management. You can create projects anywhere:

mkdir myproject
cd myproject
go mod init github.com/yourusername/myproject

This creates a go.mod file that tracks your dependencies.

Your First Go Program

Create a file named main.go:

package main

import "fmt"

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

Understanding the Code

  1. package main: Every Go file belongs to a package. main is special - it's the entry point
  2. import "fmt": Import the format package from the standard library
  3. func main(): The main function is where execution begins
  4. fmt.Println(): Print a line to stdout

Running Your Program

# Run directly
go run main.go

# Build an executable
go build main.go
./main

# Build and install to $GOPATH/bin
go install

Essential Go Commands

# Run a program
go run main.go

# Build a binary
go build

# Build for a specific platform
GOOS=linux GOARCH=amd64 go build

# Format code (automatically fixes style)
go fmt ./...

# Check for errors and issues
go vet ./...

# Download dependencies
go mod tidy

# Run tests
go test ./...

# Get documentation
go doc fmt.Println

# Initialize a new module
go mod init <module-name>

Editor Setup

  1. Install the official Go extension by the Go team
  2. It provides:
    • Auto-completion
    • Formatting on save
    • Debugging support
    • Test running
    • Import management

Other Editors

  • GoLand: Full-featured IDE from JetBrains
  • Vim/Neovim: Use vim-go or gopls (LSP)
  • Emacs: go-mode with gopls

Go Philosophy

Understanding Go's philosophy will help you write idiomatic code:

  1. "Less is more": Go deliberately omits features (generics were added late, no classes, no exceptions)
  2. "Clear is better than clever": Write obvious, readable code
  3. "Don't communicate by sharing memory; share memory by communicating": Use channels for concurrency
  4. "A little copying is better than a little dependency": Don't over-abstract
  5. "Errors are values": Handle them explicitly, don't throw exceptions

Next Steps

Continue to 02-basics.md to learn about variables, types, and basic syntax.