Lifecycle Hooks

Handle application startup and shutdown events.

Running

go run main.go

Server starts on http://localhost:3070

Press Ctrl+C to trigger shutdown hook.

Hooks Implemented

Startup Hook

func OnStartup() {
    log.Println("🚀 Application starting...")
    startTime = time.Now()
    // Initialize resources
}

Called when:

Shutdown Hook

func OnShutdown() {
    uptime := time.Since(startTime)
    log.Printf("⏱️ Uptime: %v", uptime)
    // Cleanup resources
}

Called when:

Use Cases

Pattern

func main() {
    OnStartup()           // Initialize
    defer OnShutdown()    // Cleanup
    
    app.Run(timeout)      // Run app
}