{"id":822,"date":"2026-10-09T09:00:00","date_gmt":"2026-10-09T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/"},"modified":"2026-11-29T01:29:29","modified_gmt":"2026-11-28T19:59:29","slug":"go-goroutine-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/","title":{"rendered":"Go Goroutine Programs with Output: WaitGroup, Once and Results"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go goroutine programs run several pieces of work at the same time, which is the feature Go is most famous for. A goroutine is a very light thread managed by Go, so you can start thousands of them without slowing your machine. In this post, you will write twelve goroutine programs with their exact output, with the logic explained in code comments, including the interview favourite: print odd and even numbers alternately with two goroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on our <a href=\"https:\/\/smitabharat.com\/blog\/go-function-programs\/\">Go function programs<\/a>, because every goroutine runs a function. For the theory, read the <a href=\"https:\/\/smitabharat.com\/blog\/go-goroutines-beginners-guide\/\">Go goroutines guide for beginners<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\">WaitGroup and Mutex explained<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Goroutine Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go goroutine programs start a function with the <code>go<\/code> keyword, so it runs alongside the rest of the program. The Go runtime decides which goroutine runs on which CPU core, and switches between them very quickly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a restaurant kitchen. One cook making every dish one by one is slow. With goroutines, you hire many cooks who work at the same time, and the head chef (the Go runtime) tells each one what to do. A <code>WaitGroup<\/code> is the head chef waiting until every dish is ready before serving.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Goroutine Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Modern servers handle thousands of requests at once. For example, a web server starts one goroutine per request, a crawler fetches many pages in parallel and a report job processes files on all CPU cores. So Go goroutine programs are at the heart of Go backend work.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Speed:<\/strong> split big work across CPU cores.<\/li>\n\n\n<li><strong>Waiting:<\/strong> do other work while one task waits for the network or disk.<\/li>\n\n\n<li><strong>Interviews:<\/strong> odd-even printing, WaitGroup bugs and deadlocks are very common questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Goroutine Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>go f()<\/code> starts <code>f<\/code> in a new goroutine and returns at once. The <code>main<\/code> function does not wait for it. So we use a <code>sync.WaitGroup<\/code>: we tell it how many goroutines to wait for, each goroutine says &#8220;done&#8221; at the end, and <code>Wait<\/code> blocks until the count reaches zero.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-1024x640.png\" alt=\"Go goroutine programs diagram showing main starting goroutines and waiting with a WaitGroup\" class=\"wp-image-820\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">main starts several goroutines with wg.Go, each is marked done when it finishes, and wg.Wait blocks until all of them are done.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the goroutine tools in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Tool<\/th><th>What it does<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>go f()<\/td><td>Starts f in a new goroutine<\/td><td>Returns immediately<\/td><\/tr><tr><td>sync.WaitGroup<\/td><td>Waits for many goroutines<\/td><td>Add, Done, Wait<\/td><\/tr><tr><td>wg.Go(f)<\/td><td>Add, go and Done in one call<\/td><td>Since Go 1.25<\/td><\/tr><tr><td>sync.Once<\/td><td>Runs code only once<\/td><td>Safe across goroutines<\/td><\/tr><tr><td>runtime.NumGoroutine<\/td><td>Counts live goroutines<\/td><td>Useful for leak checks<\/td><\/tr><tr><td>go run -race<\/td><td>Finds data races<\/td><td>Use in testing<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Goroutine Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go goroutine programs start goroutines and wait for them. If goroutines are new to you, begin with Program 1. It starts just one goroutine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Your First Goroutine<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"  \/\/ fmt prints text on the screen\n    \"sync\" \/\/ sync has tools for waiting on goroutines\n)\n\n\/\/ greet prints a hello message. It will run inside a goroutine.\nfunc greet(name string, wg *sync.WaitGroup) {\n    defer wg.Done() \/\/ defer runs this when greet ends; Done says \"I am finished\"\n    fmt.Println(\"Hello,\", name)\n}\n\nfunc main() {\n    \/\/ wg (wait group) counts the goroutines we must wait for.\n    var wg sync.WaitGroup\n\n    wg.Add(1)             \/\/ we are about to start one goroutine\n    go greet(\"Asha\", &amp;wg) \/\/ go runs greet in a new goroutine; &amp;wg passes a pointer to wg\n    wg.Wait()             \/\/ wait here until greet calls Done\n\n    fmt.Println(\"main is done\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Asha\nmain is done<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>wg.Add(1)<\/code> tells the WaitGroup that one goroutine is coming. Then the <code>go<\/code> keyword starts <code>greet<\/code> in the background, and <code>main<\/code> moves on to <code>wg.Wait()<\/code>. When <code>greet<\/code> calls <code>Done<\/code>, the wait ends and <code>main<\/code> prints its last line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Start Goroutines and Wait with WaitGroup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    \/\/ make creates a slice with 3 empty strings. Each goroutine writes only its own slot.\n    results := make([]string, 3)\n\n    for i := range 3 { \/\/ i takes the values 0, 1 and 2\n        wg.Add(1) \/\/ tell the WaitGroup one more goroutine is coming\n        go func() {\n            defer wg.Done() \/\/ say \"finished\" when this goroutine ends\n            \/\/ Sprintf builds a string instead of printing it.\n            results[i] = fmt.Sprintf(\"task %d done\", i+1)\n        }()\n    }\n\n    wg.Wait() \/\/ block here until all three called Done\n    for _, result := range results {\n        fmt.Println(result)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>task 1 done\ntask 2 done\ntask 3 done<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop starts three small goroutines, and each one fills its own slot. Goroutines may finish in any order. We keep the output stable by writing into fixed slots and printing after <code>Wait<\/code>. Since Go 1.22, each loop pass has its own <code>i<\/code>, so every goroutine sees the right number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: The Shorter wg.Go Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go 1.25 added <code>wg.Go<\/code>, which does <code>Add<\/code>, <code>go<\/code> and <code>Done<\/code> for you.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    squares := make([]int, 5)\n\n    for i := range 5 {\n        \/\/ wg.Go starts the function in a new goroutine and calls Done for you (Go 1.25+).\n        wg.Go(func() {\n            squares[i] = (i + 1) * (i + 1)\n        })\n    }\n    wg.Wait()\n    fmt.Println(squares)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 4 9 16 25]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each goroutine squares one number and stores it in its own slot. <code>wg.Go<\/code> counts the goroutine and marks it done for you. This form removes the most common WaitGroup bug, which is forgetting <code>Add<\/code> or <code>Done<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: What Happens Without Waiting<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    go fmt.Println(\"from goroutine\") \/\/ may never get a chance to run\n    fmt.Println(\"main is finishing\")\n    \/\/ No wait here. When main returns, the whole program stops.\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Most runs print only this line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main is finishing<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>main<\/code> returns, every goroutine is stopped at once. The goroutine usually has no time to print, which is why you always need a <code>WaitGroup<\/code> or a channel. On a busy machine it might print, so never rely on either result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parallel Work in Go Goroutine Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go goroutine programs split one big job into parts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Sum a Slice in Parallel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    \/\/ Step 1: fill a slice with the numbers 1 to 1000.\n    numbers := make([]int, 1000)\n    for i := range numbers {\n        numbers[i] = i + 1\n    }\n\n    \/\/ Step 2: split the work into 4 parts, one goroutine each.\n    parts := 4\n    partial := make([]int, parts) \/\/ one result slot per goroutine\n    size := len(numbers) \/ parts  \/\/ 250 numbers per part\n    var wg sync.WaitGroup\n\n    for part := range parts {\n        wg.Go(func() {\n            \/\/ Each goroutine sums its own chunk of the slice.\n            start := part * size\n            end := start + size\n            for _, number := range numbers[start:end] { \/\/ [start:end] takes a piece of the slice\n                partial[part] += number\n            }\n        })\n    }\n    wg.Wait()\n\n    \/\/ Step 3: combine the partial sums in main.\n    total := 0\n    for _, sum := range partial {\n        total += sum\n    }\n    fmt.Println(partial, total)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[31375 93875 156375 218875] 500500<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each goroutine adds up 250 numbers and writes only its own <code>partial[part]<\/code>. So there is no shared counter and no data race. Finally, <code>main<\/code> adds the four parts, and the total matches the formula 1000 x 1001 \/ 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Get One Result Back with a Channel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ addNumbers adds two numbers and sends the answer into the channel.\nfunc addNumbers(first, second int, result chan int) {\n    result &lt;- first + second \/\/ &lt;- sends a value into the channel\n}\n\nfunc main() {\n    \/\/ A channel is a pipe that goroutines use to pass values.\n    result := make(chan int)\n\n    go addNumbers(20, 22, result) \/\/ runs in a new goroutine\n\n    \/\/ &lt;-result waits until a value arrives, then reads it.\n    sum := &lt;-result\n    fmt.Println(\"Sum:\", sum)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 42<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the easiest way to get an answer back from a goroutine. The goroutine sends the sum into the channel. Meanwhile, <code>main<\/code> waits at <code>&lt;-result<\/code> until the value arrives, so no WaitGroup is needed here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Collect Results with a Channel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n    \"sync\"\n)\n\nfunc main() {\n    words := []string{\"go\", \"rust\", \"python\", \"c\"}\n    lengths := make(chan int, len(words)) \/\/ buffered: it can hold all results\n    var wg sync.WaitGroup\n\n    for _, word := range words {\n        wg.Go(func() {\n            lengths &lt;- len(word) \/\/ send each result into the channel\n        })\n    }\n    wg.Wait()      \/\/ wait until every goroutine has sent\n    close(lengths) \/\/ no more values, so range can stop\n\n    var all []int\n    for length := range lengths { \/\/ read every value from the channel\n        all = append(all, length) \/\/ append adds a value to the end of the slice\n    }\n    slices.Sort(all) \/\/ arrival order is random, so sort\n    fmt.Println(all)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 4 6]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The buffered channel lets every goroutine send without waiting. We close it after <code>Wait<\/code>, so the <code>range<\/code> loop knows when to stop. Then sorting makes the output the same every time. Channels get their own post next.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: sync.Once for One-Time Setup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var once sync.Once\n    var wg sync.WaitGroup\n    setupCount := 0\n\n    for range 10 { \/\/ start 10 goroutines\n        wg.Go(func() {\n            \/\/ Only the first caller runs the function; others wait for it.\n            once.Do(func() {\n                setupCount++\n            })\n        })\n    }\n    wg.Wait()\n    fmt.Println(\"setup ran\", setupCount, \"time\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>setup ran 1 time<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ten goroutines all call <code>once.Do<\/code>, but only the first call runs the setup. The other nine skip it. <code>sync.Once<\/code> is perfect for loading a config or opening a database connection the first time it is needed, even when many goroutines ask at once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Go Goroutine Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go goroutine programs come up in interviews again and again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Print Odd and Even Numbers Alternately<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Two goroutines take turns. Each one waits for its turn on a channel, prints, and hands the turn to the other.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    const limit = 10\n    oddTurn := make(chan bool)  \/\/ a signal: \"odd, it is your turn\"\n    evenTurn := make(chan bool) \/\/ a signal: \"even, it is your turn\"\n    var wg sync.WaitGroup\n\n    \/\/ Odd printer: prints 1, 3, 5, ...\n    wg.Go(func() {\n        for i := 1; i &lt;= limit; i += 2 {\n            &lt;-oddTurn \/\/ wait for my turn\n            fmt.Println(\"odd :\", i)\n            evenTurn &lt;- true \/\/ give the turn to the even printer\n        }\n    })\n\n    \/\/ Even printer: prints 2, 4, 6, ...\n    wg.Go(func() {\n        for i := 2; i &lt;= limit; i += 2 {\n            &lt;-evenTurn \/\/ wait for my turn\n            fmt.Println(\"even:\", i)\n            if i &lt; limit {\n                oddTurn &lt;- true \/\/ hand back unless we are finished\n            }\n        }\n    })\n\n    oddTurn &lt;- true \/\/ start the game: odd goes first\n    wg.Wait()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>odd : 1\neven: 2\nodd : 3\neven: 4\nodd : 5\neven: 6\nodd : 7\neven: 8\nodd : 9\neven: 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unbuffered channels act like a baton in a relay race. Only one goroutine holds the turn at a time, so the order is always correct. The <code>if i &lt; limit<\/code> check stops the even printer from handing a turn that nobody will take.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Download Simulator with Many Workers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\n\/\/ download pretends to download a file by waiting for a short time.\nfunc download(file string, delayMs int) string {\n    time.Sleep(time.Duration(delayMs) * time.Millisecond) \/\/ pause like a slow network\n    return file + \" ok\"\n}\n\nfunc main() {\n    files := []string{\"a.pdf\", \"b.jpg\", \"c.zip\"}\n    delays := []int{300, 100, 200}\n    results := make([]string, len(files))\n    start := time.Now()\n\n    var wg sync.WaitGroup\n    for i, file := range files {\n        wg.Go(func() {\n            results[i] = download(file, delays[i])\n        })\n    }\n    wg.Wait()\n\n    fmt.Println(results)\n    \/\/ All three run at the same time, so the total is about the slowest one.\n    fmt.Println(\"under 400ms:\", time.Since(start) &lt; 400*time.Millisecond)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[a.pdf ok b.jpg ok c.zip ok]\nunder 400ms: true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each file gets its own goroutine and its own result slot. Run one after another, the downloads would take 600 ms. In parallel, they take about 300 ms, which is the time of the slowest file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Count Goroutines Before and After<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n    \"sync\"\n)\n\nfunc main() {\n    fmt.Println(\"at start:\", runtime.NumGoroutine()) \/\/ just main\n\n    var wg sync.WaitGroup\n    stop := make(chan struct{}) \/\/ struct{} carries no data, it is only a signal\n    for range 5 {\n        wg.Go(func() {\n            &lt;-stop \/\/ block until we close the channel\n        })\n    }\n    fmt.Println(\"while running:\", runtime.NumGoroutine() &gt;= 6) \/\/ main plus 5 more\n\n    close(stop) \/\/ wake every goroutine at once\n    wg.Wait()\n    fmt.Println(\"finished cleanly\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>at start: 1\nwhile running: true\nfinished cleanly<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Five goroutines wait on the <code>stop<\/code> channel, so the count grows to at least six. Closing a channel wakes every goroutine waiting on it. Checking <code>NumGoroutine<\/code> in tests is a simple way to spot goroutine leaks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: A Goroutine Panic Recovered Safely<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\n\/\/ safeGo runs a job in a goroutine and catches any panic inside it.\nfunc safeGo(wg *sync.WaitGroup, name string, job func()) {\n    wg.Add(1)\n    go func() {\n        defer wg.Done()\n        \/\/ This deferred function runs last, even after a panic.\n        defer func() {\n            \/\/ recover only works inside the same goroutine that panicked.\n            problem := recover() \/\/ nil if there was no panic\n            if problem != nil {\n                fmt.Println(name, \"recovered:\", problem)\n            }\n        }()\n        job()\n    }()\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    safeGo(&amp;wg, \"job-1\", func() {\n        var scores map[string]int \/\/ a nil map, never created with make\n        scores[\"x\"] = 1           \/\/ panics: you cannot write to a nil map\n    })\n    wg.Wait()\n    fmt.Println(\"main still running\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>job-1 recovered: assignment to entry in nil map\nmain still running<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The job writes to a nil map, which causes a panic. The deferred function catches it with <code>recover<\/code> and prints a message instead. A panic in any goroutine crashes the whole program unless that goroutine recovers it, so this wrapper protects every background job.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sequential vs Concurrent Go Goroutine Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most Go goroutine programs start from one question: one by one, or at the same time? The diagram and table below compare the two.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-1024x640.png\" alt=\"Go goroutine programs comparison of sequential and concurrent work\" class=\"wp-image-821\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Sequential code runs tasks one after another, while goroutines run them at the same time and finish when the slowest task ends.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Sequential<\/th><th>Goroutines<\/th><\/tr><\/thead><tbody><tr><td>Total time for waits<\/td><td>Sum of all tasks<\/td><td>About the slowest task<\/td><\/tr><tr><td>Order of results<\/td><td>Fixed<\/td><td>Random unless you control it<\/td><\/tr><tr><td>Shared data<\/td><td>Safe<\/td><td>Needs channels or a mutex<\/td><\/tr><tr><td>Code complexity<\/td><td>Simple<\/td><td>A little more<\/td><\/tr><tr><td>Best for<\/td><td>Short, dependent steps<\/td><td>Many independent tasks<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So use goroutines when tasks are independent and spend time waiting. Keep code sequential when each step depends on the one before.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Goroutine Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Not waiting:<\/strong> <code>main<\/code> exits and kills every goroutine, so use a <code>WaitGroup<\/code> or channel.<\/li>\n\n\n<li><strong>wg.Add inside the goroutine:<\/strong> <code>Wait<\/code> may run before <code>Add<\/code>. Call <code>Add<\/code> before <code>go<\/code>, or use <code>wg.Go<\/code>.<\/li>\n\n\n<li><strong>Copying a WaitGroup:<\/strong> pass it as <code>*sync.WaitGroup<\/code>, or <code>Done<\/code> changes a copy and <code>Wait<\/code> never returns.<\/li>\n\n\n<li><strong>Shared counters without a lock:<\/strong> many goroutines writing one variable cause a data race.<\/li>\n\n\n<li><strong>Forgetting to stop goroutines:<\/strong> goroutines that block forever leak memory.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Goroutine Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Prefer wg.Go<\/strong> on Go 1.25 and later, since it cannot forget Add or Done.<\/li>\n\n\n<li><strong>Give each goroutine its own result slot<\/strong>, or send results on a channel.<\/li>\n\n\n<li><strong>Run tests with -race<\/strong>, as shown in our <a href=\"https:\/\/smitabharat.com\/blog\/go-race-detector-data-races\/\">Go race detector guide<\/a>.<\/li>\n\n\n<li><strong>Recover inside long-running goroutines<\/strong>, so one bug does not crash the server.<\/li>\n\n\n<li><strong>Always have a way to stop goroutines<\/strong>, such as closing a channel or a context.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Goroutine Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers ask these Go goroutine programs more than any other concurrency questions. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basics Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What is the difference between a goroutine and an OS thread?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A goroutine starts with a tiny stack of a few kilobytes that grows as needed, while an OS thread usually reserves about 1 MB. The Go runtime runs many goroutines on a few threads, so starting 100,000 goroutines is normal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := make(chan int) \/\/ unbuffered channel\n    numbers &lt;- 1              \/\/ nobody is receiving, so this waits forever\n    fmt.Println(&lt;-numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fatal error: all goroutines are asleep - deadlock!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An unbuffered send waits for a receiver. <code>main<\/code> is the only goroutine, and it is stuck sending, so Go detects a deadlock and stops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What is wrong with passing a WaitGroup by value?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\n\/\/ work gets a copy of the WaitGroup, not the real one.\nfunc work(wg sync.WaitGroup) {\n    defer wg.Done()\n    fmt.Println(\"working\")\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    wg.Add(1)\n    go work(wg)\n    wg.Wait()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>fatal error: all goroutines are asleep - deadlock!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Done<\/code> lowers the counter of the copy, so the real counter stays at 1 and <code>Wait<\/code> never returns. Pass <code>&amp;wg<\/code> and take <code>*sync.WaitGroup<\/code>. <code>go vet<\/code> also warns about this copy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. Does a goroutine run on a new CPU core?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not always. The Go scheduler decides. With <code>GOMAXPROCS<\/code> set to the number of cores (the default), goroutines can run in parallel, but they may also share one core and take turns.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions on Go Goroutine Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var mu sync.Mutex \/\/ mu (mutex) lets only one goroutine in at a time\n    var wg sync.WaitGroup\n    count := 0\n    for range 1000 {\n        wg.Go(func() {\n            mu.Lock()\n            count++ \/\/ only one goroutine at a time gets here\n            mu.Unlock()\n        })\n    }\n    wg.Wait()\n    fmt.Println(count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without the mutex, the result could be less than 1000, because two goroutines could read the same old value. Mutexes get their own post soon.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    results := make([]int, 3)\n    for i := range 3 {\n        wg.Go(func() {\n            results[i] = i * 10\n        })\n    }\n    wg.Wait()\n    fmt.Println(results)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[0 10 20]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each goroutine captures its own <code>i<\/code> (Go 1.22 and later), so every slot gets the right value. That is why the slice holds 0, 10 and 20.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. How many goroutines does a plain program start with?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One, the goroutine running <code>main<\/code>. The runtime also has some system goroutines, but <code>runtime.NumGoroutine()<\/code> reports 1 at the start of a simple program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nfunc main() {\n    var wg sync.WaitGroup\n    done := make(chan string, 2) \/\/ room for two messages\n\n    wg.Go(func() {\n        done &lt;- \"first\"\n    })\n    wg.Wait() \/\/ \"first\" is surely sent before we go on\n\n    wg.Go(func() {\n        done &lt;- \"second\"\n    })\n    wg.Wait()\n\n    close(done)\n    for message := range done {\n        fmt.Println(message)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>first\nsecond<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first <code>Wait<\/code> makes sure &#8220;first&#8221; is sent before the second goroutine starts, so the order is fixed. A WaitGroup can be reused after <code>Wait<\/code> returns.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Design Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. How do you limit how many goroutines run at once?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use a buffered channel as a semaphore: send before starting work and receive when done. With a capacity of 3, only three goroutines can do the work at the same time. A worker pool is another common answer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. How do you return an error from a goroutine?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Send it on a channel, or store it in a slot like the results in Program 2. The <code>errgroup<\/code> package from <code>golang.org\/x\/sync<\/code> does this neatly and also cancels the other goroutines on the first error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What is a goroutine leak?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a goroutine that never ends, usually because it waits forever on a channel nobody uses. Leaks slowly eat memory, so always give goroutines a way to stop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. When should you not use goroutines?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the work is tiny, like adding two numbers, the cost of starting and syncing goroutines is bigger than the gain. Also avoid them when every step depends on the previous one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task is a small version of real Go goroutine programs. Write a program that checks five website names concurrently by &#8220;pinging&#8221; each with a fake delay, and prints which ones answered in under 200 ms. Hint: use <code>wg.Go<\/code>, store each result in its own slot, and print after <code>wg.Wait()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/tour\/concurrency\/1\">A Tour of Go: Goroutines<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/sync#WaitGroup\">sync package: WaitGroup<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/effective_go#concurrency\">Effective Go: concurrency<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go goroutine programs start work with <code>go f()<\/code>, and it runs alongside <code>main<\/code>.<\/li>\n\n\n<li>When <code>main<\/code> returns, all goroutines stop, so wait with a <code>WaitGroup<\/code> or a channel.<\/li>\n\n\n<li><code>wg.Go<\/code> (Go 1.25+) handles Add, go and Done in one call.<\/li>\n\n\n<li>Give each goroutine its own result slot, or use a channel or mutex for shared data.<\/li>\n\n\n<li>A panic in a goroutine crashes the program unless that goroutine recovers.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, goroutines start talking to each other. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-channel-programs\/\">Go channel programs with output<\/a>, with buffered channels, select, timeouts and producer-consumer. For more on WaitGroup, see <a href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\">Go sync package: WaitGroup and Mutex<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go goroutine programs run several pieces of work at the same time, which is the feature Go is most famous for. A goroutine is&hellip;<\/p>\n","protected":false},"author":1,"featured_media":819,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,36,95,11,104],"swapz_series":[],"class_list":["post-822","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-goroutines","tag-go-interview-questions","tag-go-lang","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Goroutine Programs: WaitGroup, Once, Results<\/title>\n<meta name=\"description\" content=\"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Goroutine Programs: WaitGroup, Once, Results\" \/>\n<meta property=\"og:description\" content=\"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-09T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Goroutine Programs with Output: WaitGroup, Once and Results\",\"datePublished\":\"2026-10-09T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/\"},\"wordCount\":1938,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-goroutine-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Goroutines\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/\",\"name\":\"Go Goroutine Programs: WaitGroup, Once, Results\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-goroutine-programs-featured.png\",\"datePublished\":\"2026-10-09T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:29+00:00\",\"description\":\"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-goroutine-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-goroutine-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Goroutine Programs featured image: WaitGroup and goroutine results output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-goroutine-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Goroutine Programs with Output: WaitGroup, Once and Results\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Goroutine Programs: WaitGroup, Once, Results","description":"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Goroutine Programs: WaitGroup, Once, Results","og_description":"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&A.","og_url":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-09T03:30:00+00:00","article_modified_time":"2026-11-28T19:59:29+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Goroutine Programs with Output: WaitGroup, Once and Results","datePublished":"2026-10-09T03:30:00+00:00","dateModified":"2026-11-28T19:59:29+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/"},"wordCount":1938,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png","keywords":["Beginner","Go Goroutines","Go Interview Questions","Go Lang","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/","name":"Go Goroutine Programs: WaitGroup, Once, Results","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png","datePublished":"2026-10-09T03:30:00+00:00","dateModified":"2026-11-28T19:59:29+00:00","description":"Go goroutine programs with output: start goroutines, wait with WaitGroup, collect results, run once, odd even printing, plus goroutine interview Q&A.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-goroutine-programs-featured.png","width":2400,"height":1260,"caption":"Go Goroutine Programs featured image: WaitGroup and goroutine results output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-goroutine-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Goroutine Programs with Output: WaitGroup, Once and Results"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=822"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"predecessor-version":[{"id":894,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/822\/revisions\/894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/819"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=822"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}