Zum Hauptinhalt springen

Go Core Concepts

Object Model / Type System

Interfaces

  • Interface definition and implementation
  • Empty interface
  • Type assertions and type switches
  • Best practices for interface design

Structs

  • Struct definition
  • Embedding and composition
  • Methods and receivers
  • Tags and reflection

Type System Features

  • Type aliases
  • Type embedding
  • Type constraints (Go 1.18+)
  • Type inference

Memory Management

Memory Model

  • Stack vs Heap allocation
  • Value vs Pointer semantics
  • Escape analysis
  • Memory layout and alignment

Garbage Collection

  • GC algorithm overview
  • GC tuning and optimization
  • Memory profiling
  • Best practices for memory management

Concurrency Pattern

Goroutine Patterns

  • Worker pools
  • Pipeline patterns
  • Fan-out/Fan-in
  • Rate limiting

Channel Patterns

  • Generator pattern
  • Multiplexing with select
  • Done channel pattern
  • Error handling in concurrent code
// Generator pattern example
func fibonacci(n int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
a, b := 0, 1
for i := 0; i < n; i++ {
out <- a
a, b = b, a+b
}
}()
return out
}

// Multiplexing with select
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
wg.Add(len(cs))

for _, c := range cs {
go func(ch <-chan int) {
defer wg.Done()
for n := range ch {
out <- n
}
}(c)
}

go func() {
wg.Wait()
close(out)
}()

return out
}

// Usage example
func main() {
ch1 := fibonacci(5)
ch2 := fibonacci(3)
for n := range merge(ch1, ch2) {
fmt.Println(n)
}
}

Synchronization Patterns

  • Mutex patterns
  • Once patterns
  • Atomic operations
  • Context usage

I/O Model

I/O Interfaces

  • io.Reader and io.Writer
  • io.ReadWriter
  • bufio package
  • ioutil utilities

File Operations

  • File reading and writing
  • Directory operations
  • File system abstractions
  • Temporary files

Network I/O

  • TCP/UDP networking
  • HTTP client/server
  • WebSocket support
  • Connection pooling

Exception Handling

Error Handling Patterns

  1. Basic Error Handling

    if err != nil {
    return fmt.Errorf("operation failed: %w", err)
    }
  2. Custom Error Types

    type CustomError struct {
    Code int
    Message string
    }

    func (e *CustomError) Error() string {
    return fmt.Sprintf("error %d: %s", e.Code, e.Message)
    }

Panic and Recovery

  • When to use panic
  • Recover mechanism
  • Defer patterns
  • Error vs Panic