Go Introduction

Go (Golang) is a statically typed, compiled language designed at Google. It emphasizes simplicity, performance, and concurrency — making it perfect for servers, microservices, CLI tools, and cloud infrastructure.

Why Go?

  • Simple — Small language, easy to learn (1-2 weeks to productive)
  • Fast compilation — Compiles in seconds, even for large projects
  • Fast execution — Compiled to native machine code
  • Built-in concurrency — Goroutines and channels make parallelism easy
  • Standard library — HTTP server, JSON, testing, crypto — all included
  • Static binaries — Single executable, no dependencies needed
  • Used by — Docker, Kubernetes, Terraform, GitHub, Uber, Cloudflare

Hello World

Go
package main

import "fmt"

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

Variables and Types

Go
// Explicit types
var name string = "Alice"
var age int = 28
var pi float64 = 3.14159
var active bool = true

// Type inference (:= short declaration)
name := "Alice"
age := 28
pi := 3.14159

// Multiple declarations
var (
    x int     = 10
    y string  = "hello"
    z float64 = 3.14
)

// Constants
const Pi = 3.14159265358979
const MaxRetries = 3

// iota (enum-like)
type Color int

const (
    Red Color = iota  // 0
    Green             // 1
    Blue              // 2
)

// Zero values
var s string    // ""
var n int       // 0
var f float64   // 0.0
var b bool      // false
var p *int      // nil

Functions

Go
// Basic function
func add(a int, b int) int {
    return a + b
}

// Shorthand parameter types
func multiply(a, b int) int {
    return a * b
}

// Multiple return values
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

// Named return values
func swap(a, b int) (x, y int) {
    x = b
    y = a
    return  // Naked return
}

// Error handling pattern
result, err := divide(10, 3)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)

// Variadic functions
func sum(nums ...int) int {
    total := 0
    for _, n := range nums {
        total += n
    }
    return total
}
sum(1, 2, 3, 4, 5)  // 15

Control Flow

Go
// if/else
if score >= 90 {
    fmt.Println("A")
} else if score >= 80 {
    fmt.Println("B")
} else {
    fmt.Println("F")
}

// if with initialization
if err := doSomething(); err != nil {
    log.Fatal(err)
}

// switch
switch day {
case "Monday":
    fmt.Println("Start of week")
case "Friday":
    fmt.Println("Almost weekend")
default:
    fmt.Println("Regular day")
}

// for loop (Go has only for)
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// While-style
for count < 10 {
    count++
}

// Infinite loop
for {
    select {
    case msg := <-ch:
        fmt.Println(msg)
    case <-time.After(time.Second):
        return
    }
}

// Range
for i, v := range slice {
    fmt.Printf("index=%d, value=%d\n", i, v)
}

for k, v := range myMap {
    fmt.Printf("key=%s, value=%d\n", k, v)
}

Structs and Methods

Go
// Struct
type Person struct {
    Name string
    Email string
    Age  int
}

// Method
func (p Person) Greet() string {
    return fmt.Sprintf("Hi, I'm %s!", p.Name)
}

// Pointer receiver (can modify)
func (p *Person) SetAge(age int) {
    p.Age = age
}

// Usage
alice := Person{Name: "Alice", Email: "alice@example.com", Age: 28}
alice.SetAge(29)
fmt.Println(alice.Greet())

Concurrency

Go
// Goroutine (lightweight thread)
go func() {
    fmt.Println("Running in background")
}()

// Channel (communication between goroutines)
ch := make(chan int)

go func() {
    ch <- 42  // Send
}()

value := <-ch  // Receive
fmt.Println(value)

// Buffered channel
ch := make(chan int, 10)
ch <- 1
ch <- 2
fmt.Println(<-ch)  // 1
fmt.Println(<-ch)  // 2

// Select (multiple channels)
select {
case msg := <-ch1:
    fmt.Println(msg)
case msg := <-ch2:
    fmt.Println(msg)
case <-time.After(time.Second):
    fmt.Println("timeout")
}

💡 Tip: Go's simplicity is its strength. Resist the urge to add abstractions — embrace if err != nil checks, keep functions small, and let the standard library do the heavy lifting.

Next: Go Types and Interfaces