Back to Home
    GoGo 1.23Intermediate

    Go

    Go syntax, goroutines, channels, error handling, interfaces, and standard library patterns.

    12 min read
    gogolangbackendconcurrency

    Basics

    1 topic

    Variables & Types

    go
    package main
    
    import "fmt"
    
    func main() {
        // var declaration
        var name string = "Alice"
        var age int = 30
    
        // Short declaration (preferred inside functions)
        city := "NYC"
        count, err := getCount()
    
        // Constants
        const Pi = 3.14159
        const (
            StatusOK    = 200
            StatusNotFound = 404
        )
    
        // Zero values: 0, "", false, nil
        var n int     // 0
        var s string  // ""
        var b bool    // false
    
        fmt.Println(name, age, city, count, err, n, s, b)
    }

    💡 Use := for local vars — var is for package-level or when type needs to be explicit

    Error Handling

    1 topic

    Errors in Go

    go
    import (
        "errors"
        "fmt"
    )
    
    // Return error as last value (convention)
    func divide(a, b float64) (float64, error) {
        if b == 0 {
            return 0, errors.New("division by zero")
        }
        return a / b, nil
    }
    
    // Custom error type
    type NotFoundError struct {
        ID int
    }
    func (e *NotFoundError) Error() string {
        return fmt.Sprintf("item %d not found", e.ID)
    }
    
    // Wrapping errors (Go 1.13+)
    if err != nil {
        return fmt.Errorf("fetchUser: %w", err)
    }
    
    // Unwrap
    var nfe *NotFoundError
    if errors.As(err, &nfe) {
        fmt.Println("Not found:", nfe.ID)
    }

    💡 Always handle errors — never ignore with _

    ⚡ %w wraps errors so callers can use errors.As/Is to inspect the chain

    Concurrency

    1 topic

    Goroutines & Channels

    go
    import "sync"
    
    // Goroutine
    go func() {
        fmt.Println("concurrent")
    }()
    
    // Channel
    ch := make(chan int, 10) // buffered
    go func() { ch <- 42 }()
    val := <-ch
    
    // WaitGroup
    var wg sync.WaitGroup
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func(n int) {
            defer wg.Done()
            process(n)
        }(i)
    }
    wg.Wait()
    
    // Mutex for shared state
    var mu sync.Mutex
    mu.Lock()
    counter++
    mu.Unlock()
    
    // select: multiplex channels
    select {
    case msg := <-ch1:
        handle(msg)
    case <-timeout:
        handleTimeout()
    }

    💡 Don't communicate by sharing memory — share memory by communicating (channels)

    ⚠️ Always pair Add() before the goroutine starts, not inside it

    Related Articles

    Background reading and deeper explanations for this sheet.

    Build an Agentic App with FastAPI and Azure AI Foundry: A Practical Beginner-to-Pro Guide

    Learn how to design, build, and deploy an agentic application using FastAPI and Azure AI Foundry with practical, real-world examples. This guide walks you from architecture and setup to tool-calling, memory, observability, and production deployment patterns.

    keyword overlap

    Evaluation of LLM Systems: 12 Overlooked Metrics That Actually Predict Production Success

    Most LLM teams track accuracy and latency, then wonder why users still churn. This practical guide covers underused evaluation metrics that reveal real-world quality, safety, cost, and reliability issues before they hit production. You’ll get concrete formulas, examples, and implementation patterns you can apply immediately.

    keyword overlap

    Handling AI Hallucinations in Real Applications: A Practical Guide for Developers and Product Teams

    Hallucinations are one of the biggest blockers to shipping trustworthy AI features. In this practical guide, you’ll learn how to detect, reduce, and contain hallucinations in production with proven patterns, real-world examples, and implementation-ready code.

    keyword overlap

    Intro to FastAPI: Build a Production-Ready Python API with Real Examples

    FastAPI is one of the fastest ways to build modern APIs in Python without sacrificing code quality or developer experience. In this practical, beginner-friendly guide, you’ll learn how to create endpoints, validate data, handle errors, connect a database, secure routes, and prepare your FastAPI app for real-world deployment.

    keyword overlap