{
    "slug": "goroutine_concept",
    "term": "Goroutine-Style Concurrency",
    "category": "concurrency",
    "difficulty": "intermediate",
    "short": "Goroutines (Go) and similar lightweight green threads — coroutines, fibers — enable thousands of concurrent tasks with minimal memory, multiplexed onto OS threads by a runtime scheduler.",
    "long": "Goroutine: a lightweight, cooperatively/preemptively scheduled unit of execution in Go. Cost: ~2KB initial stack (vs ~1MB for OS thread). Go scheduler: M:N threading — maps M goroutines onto N OS threads. Similar concepts: PHP Fibers (cooperative, same thread), Swoole coroutines (cooperative, event-loop based), Python asyncio tasks, Kotlin coroutines, Java virtual threads (Project Loom). Benefits: thousands of concurrent tasks with small memory footprint. Communication: Go uses channels (CSP model). PHP equivalent: Swoole coroutines + channels, or Revolt Fiber-based async. The PHP Fiber (8.1) is the primitive; frameworks build on top.",
    "aliases": [],
    "tags": [
        "concurrency",
        "goroutines",
        "fibers",
        "coroutines",
        "green-threads"
    ],
    "misconception": "Goroutines are threads — they're scheduled by the Go runtime, not the OS. Thousands of goroutines may run on just a few OS threads.",
    "why_it_matters": "Goroutine-style concurrency enables writing sequential-looking code that's actually highly concurrent — much simpler than callback/promise chains.",
    "common_mistakes": [
        "Expecting PHP Fibers to schedule automatically — they need an event loop (Revolt, Swoole) to be useful.",
        "Treating goroutines as free — thousands are cheap, millions have overhead.",
        "Not handling goroutine/coroutine panics/errors — uncaught errors silently die."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php8_fibers_intro",
        "php_swoole_concurrency",
        "concurrency_vs_parallelism",
        "event_driven_concurrency"
    ],
    "prerequisites": [
        "php8_fibers_intro",
        "concurrency_vs_parallelism"
    ],
    "refs": [
        "https://go.dev/doc/effective_go#goroutines",
        "https://revolt.run"
    ],
    "bad_code": "// PHP Fiber without event loop — doesn't help:\n$fiber = new Fiber(function() { doWork(); });\n$fiber->start(); // Just runs synchronously",
    "good_code": "// With Revolt event loop:\nuse Revolt\\EventLoop;\nEventLoop::queue(function() { doWork1(); });\nEventLoop::queue(function() { doWork2(); }); // Runs concurrently\nEventLoop::run();",
    "quick_fix": "Use Revolt or Swoole for PHP fiber-based concurrency. Use Go goroutines + channels for idiomatic concurrent Go. Always handle errors in coroutines.",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/goroutine_concept",
        "html_url": "https://codeclaritylab.com/glossary/goroutine_concept",
        "json_url": "https://codeclaritylab.com/glossary/goroutine_concept.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Goroutine-Style Concurrency](https://codeclaritylab.com/glossary/goroutine_concept) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/goroutine_concept"
            }
        }
    }
}