{"id":739,"date":"2026-10-04T17:00:00","date_gmt":"2026-10-04T11:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/?p=739"},"modified":"2026-11-29T01:29:42","modified_gmt":"2026-11-28T19:59:42","slug":"go-loop-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/","title":{"rendered":"Go Loop Programs with Output: Table, Factorial, Fibonacci"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go loop programs let your code repeat work, such as printing a times table, adding up numbers or walking through a list. In this post, you will write fourteen loop programs with their exact output, from counting 1 to 10 up to the Fibonacci series and nested loops. Then you will practice the loop questions that interviewers ask most, including the loop variable change from Go 1.22.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-if-else-programs\/\">Go if else programs<\/a>, since most loops make a decision inside. For the theory of the <code>for<\/code> statement, read <a href=\"https:\/\/smitabharat.com\/blog\/go-for-loops-examples\/\">Go for loops explained with examples<\/a>. We tested every program on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Loop Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go loop programs repeat a block of code while a condition stays true, or once for each item in a collection. Go has only one loop keyword, <code>for<\/code>, but it can act like a classic loop, a while loop, an endless loop or a range loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a teacher calling out roll numbers. She starts at 1, calls each number, and stops after the last student. A loop does the same thing. It has a start, a stop rule and a step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Loop Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Loops are everywhere in real code, so Go loop programs are worth practising well. For example, you loop over orders to find a total, over lines in a file to count words, and over users to send emails. Most coding round problems also need at least one loop.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Totals and counts:<\/strong> sum, average and count anything in a list.<\/li>\n\n\n<li><strong>Searching:<\/strong> find the largest value or the first match.<\/li>\n\n\n<li><strong>Patterns and tables:<\/strong> nested loops print grids, tables and star patterns.<\/li>\n\n\n<li><strong>Interviews:<\/strong> factorial, Fibonacci and reverse a number are asked again and again.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Loop Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A classic <code>for<\/code> loop has three parts. First, the init statement runs once. Next, Go checks the condition before every pass. Then, after each pass, the post statement runs, and the cycle repeats until the condition is false.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-1024x640.png\" alt=\"Go loop programs flowchart showing init, condition, body and post steps\" class=\"wp-image-720\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The loop runs init once, then checks the condition, runs the body and the post statement, and repeats until the condition is false.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the four forms of <code>for<\/code> that you will use in Go loop programs.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Form<\/th><th>Syntax<\/th><th>Use it for<\/th><\/tr><\/thead><tbody><tr><td>Classic<\/td><td>for i := 1; i &lt;= 5; i++ { }<\/td><td>Counting with a step<\/td><\/tr><tr><td>While style<\/td><td>for n &gt; 0 { }<\/td><td>Repeat until something changes<\/td><\/tr><tr><td>Endless<\/td><td>for { }<\/td><td>Servers and menus, stop with break<\/td><\/tr><tr><td>Range<\/td><td>for i, v := range items { }<\/td><td>Slices, strings, maps and ints<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Counting Go Loop Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go loop programs count and add numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Print 1 to 10<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the first loop most people write. It prints the numbers 1 to 10 on one line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ i := 1 runs once, i &lt;= 10 is checked before each pass, i++ adds 1 after each pass\n    for i := 1; i &lt;= 10; i++ {\n        fmt.Print(i, \" \") \/\/ Print does not add a new line\n    }\n    fmt.Println() \/\/ end the line\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>i := 1<\/code> runs once, <code>i &lt;= 10<\/code> is checked before each pass, and <code>i++<\/code> adds one after each pass. When <code>i<\/code> becomes 11, the condition is false and the loop stops. The output ends with a space, because we print one after every number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Countdown from 5 to 1<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A loop can also count down. This easy example works like a rocket launch countdown.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Start at 5, keep going while i is 1 or more, and take away 1 each time\n    for i := 5; i &gt;= 1; i-- {\n        fmt.Println(i)\n    }\n    fmt.Println(\"Lift off!\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>5\n4\n3\n2\n1\nLift off!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop starts at 5 instead of 1. Each pass, <code>i--<\/code> takes away one. After 1, <code>i<\/code> becomes 0, the condition <code>i &gt;= 1<\/code> is false, and the program prints the last line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Sum of the First N Numbers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This program adds 1 + 2 + 3 and so on, up to 100.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    limit := 100\n    sum := 0 \/\/ start the running total at 0\n\n    for i := 1; i &lt;= limit; i++ {\n        sum += i \/\/ same as sum = sum + i\n    }\n\n    fmt.Println(\"Sum of 1 to\", limit, \"is\", sum)\n    \/\/ The school formula n x (n + 1) \/ 2 should give the same answer\n    fmt.Println(\"Formula check:\", limit*(limit+1)\/2)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum of 1 to 100 is 5050\nFormula check: 5050<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each pass adds the current number to <code>sum<\/code>. After 100 passes, <code>sum<\/code> holds the full total. The formula n(n+1)\/2 gives the same answer, which is a nice way to test your loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Multiplication Table<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This program prints the 7 times table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := 7\n\n    for i := 1; i &lt;= 10; i++ {\n        \/\/ %d prints a number; %2d and %3d pad it to 2 and 3 characters\n        fmt.Printf(\"%d x %2d = %3d\\n\", number, i, number*i)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>7 x  1 =   7\n7 x  2 =  14\n7 x  3 =  21\n7 x  4 =  28\n7 x  5 =  35\n7 x  6 =  42\n7 x  7 =  49\n7 x  8 =  56\n7 x  9 =  63\n7 x 10 =  70<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop counts from 1 to 10 and multiplies each count by 7. Here, the widths <code>%2d<\/code> and <code>%3d<\/code> keep the columns straight. We learned these in our <a href=\"https:\/\/smitabharat.com\/blog\/go-hello-world-print-programs\/\">Go print programs<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Range Over an Integer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since Go 1.22, you can range over a whole number. <code>for i := range 5<\/code> gives 0, 1, 2, 3 and 4.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ range 5 gives the numbers 0 to 4\n    for i := range 5 {\n        fmt.Print(i, \" \")\n    }\n    fmt.Println()\n\n    \/\/ If you do not need the number, leave out the variable\n    for range 3 {\n        fmt.Println(\"Hello\")\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 2 3 4\nHello\nHello\nHello<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first loop starts at 0 and stops before 5. The second loop does not need the number at all, so we leave out the variable. It simply repeats three times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classic Interview Go Loop Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These are the Go loop programs you are most likely to meet in a coding round.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Factorial of a Number<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The factorial of 5 is 5 x 4 x 3 x 2 x 1, which is 120.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := 10\n    factorial := uint64(1) \/\/ uint64 holds very big whole numbers (never negative)\n\n    \/\/ Multiply 2, 3, 4 ... up to number\n    for i := 2; i &lt;= number; i++ {\n        factorial *= uint64(i) \/\/ same as factorial = factorial * i\n    }\n\n    fmt.Printf(\"%d! = %d\\n\", number, factorial)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10! = 3628800<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We start with 1 and multiply in each number from 2 to 10. We use <code>uint64<\/code>, because factorials grow very fast. Even so, 21! is too big for 64 bits, so for larger values you need the <code>math\/big<\/code> package.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Fibonacci Series<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each Fibonacci number is the sum of the two before it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    previous := 0\n    current := 1\n\n    for i := 0; i &lt; 10; i++ {\n        fmt.Print(previous, \" \")\n\n        \/\/ Step 1: work out the next number\n        next := previous + current\n        \/\/ Step 2: move both numbers one place forward\n        previous = current\n        current = next\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 1 2 3 5 8 13 21 34<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The program always keeps the last two numbers. Each pass prints one, then adds the two to get the next number. Finally, it shifts both values forward. Many Go coders write this as <code>previous, current = current, previous+current<\/code> in one line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Reverse a Number and Count Digits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is a while-style loop. It keeps going while the number is greater than zero.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    number := 47290\n    original := number \/\/ keep a copy, because number will change\n    reversed := 0\n    digits := 0\n\n    \/\/ A for loop with only a condition works like a while loop\n    for number &gt; 0 {\n        lastDigit := number % 10           \/\/ Step 1: take the last digit\n        reversed = reversed*10 + lastDigit \/\/ Step 2: add it to the end of reversed\n        number = number \/ 10               \/\/ Step 3: remove the last digit\n        digits++\n    }\n\n    fmt.Println(\"Original:\", original)\n    fmt.Println(\"Reversed:\", reversed)\n    fmt.Println(\"Digits:\", digits)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Original: 47290\nReversed: 9274\nDigits: 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>number % 10<\/code> gives the last digit, and <code>number \/ 10<\/code> removes it. When no digits are left, the number is 0 and the loop stops. The reversed number loses the leading zero, because 09274 is just 9274.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Skip with continue and Stop with break<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>continue<\/code> jumps to the next pass. <code>break<\/code> leaves the loop at once.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    for i := 1; i &lt;= 20; i++ {\n        if i%2 == 0 {\n            continue \/\/ skip even numbers and go to the next pass\n        }\n        if i &gt; 11 {\n            break \/\/ stop the whole loop after 11\n        }\n        fmt.Print(i, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 3 5 7 9 11<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Even numbers hit <code>continue<\/code>, so they never reach the print line. When <code>i<\/code> becomes 13, the <code>break<\/code> ends the loop. That is why the output stops at 11.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Range-Based Go Loop Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In range-based Go loop programs, <code>range<\/code> walks through a collection and gives you each index and value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Print Every Item in a Shopping List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the easiest way to use <code>range<\/code>. It visits each item in a list, one by one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A slice is a list of values of the same type\n    shoppingList := []string{\"milk\", \"bread\", \"eggs\"}\n\n    \/\/ range gives the position (index) and the item on each pass\n    for index, item := range shoppingList {\n        fmt.Println(index+1, item) \/\/ +1 so the list starts at 1, not 0\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 milk\n2 bread\n3 eggs<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On each pass, <code>range<\/code> hands you the next index and item. Go counts positions from 0, so we add 1 before printing. The loop ends by itself after the last item.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Find the Largest in a Slice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This program finds the highest price and where it sits in the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    prices := []int{450, 1299, 85, 760, 1299, 300}\n\n    \/\/ Start by guessing the first price is the largest\n    largest := prices[0] \/\/ [0] reads the first item\n    position := 0\n\n    for index, price := range prices {\n        if price &gt; largest {\n            largest = price\n            position = index\n        }\n    }\n    fmt.Println(\"Largest:\", largest, \"at index\", position)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Largest: 1299 at index 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The program starts with the first price as its best guess. Each time it finds a bigger price, it updates both values. We use <code>&gt;<\/code>, not <code>&gt;=<\/code>, so the first 1299 wins.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Range Over a String<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ranging over a string gives you runes, not bytes. The index jumps when a character takes more than one byte.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A rune is one character; the euro sign uses 3 bytes\n    for index, character := range \"Go\u20ac!\" {\n        fmt.Printf(\"index %d: %c\\n\", index, character) \/\/ %c prints a character\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>index 0: G\nindex 1: o\nindex 2: \u20ac\nindex 5: !<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range<\/code> reads one full character at a time and tells you the byte position where it starts. The euro sign takes 3 bytes in UTF-8, so the next index is 5, not 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Range Over a Map in Sorted Order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Map order is random in Go. To print keys in order, sort them first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    \/\/ A map stores pairs: item name -&gt; number in stock\n    stock := map[string]int{\"pen\": 40, \"book\": 12, \"bag\": 5}\n\n    \/\/ Step 1: get all the keys and sort them A to Z\n    sortedItems := slices.Sorted(maps.Keys(stock))\n\n    \/\/ Step 2: print each item with its stock\n    for _, item := range sortedItems {\n        fmt.Println(item, stock[item]) \/\/ stock[item] looks up the value\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>bag 5\nbook 12\npen 40<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>maps.Keys<\/code> returns the keys, and <code>slices.Sorted<\/code> sorts them. Then the loop prints each key with its value. Both helpers are in the standard library since Go 1.23.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Nested Loops for a Times Grid<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A loop inside a loop is a nested loop. The inner loop runs fully for each pass of the outer one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    for row := 1; row &lt;= 4; row++ { \/\/ outer loop: one pass per row\n        for col := 1; col &lt;= 4; col++ { \/\/ inner loop: one pass per column\n            fmt.Printf(\"%4d\", row*col) \/\/ %4d pads each number to 4 characters\n        }\n        fmt.Println() \/\/ end the row\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   1   2   3   4\n   2   4   6   8\n   3   6   9  12\n   4   8  12  16<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For row 1, the inner loop prints four numbers. Then <code>fmt.Println()<\/code> ends the line, and the outer loop moves to row 2. We use nested loops a lot in our pattern programs post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classic for vs range in Go Loop Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both forms are common, so it helps to know when to pick each one. The diagram and table below compare them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-1024x640.png\" alt=\"Go loop programs comparison of classic for loops and range loops\" class=\"wp-image-721\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Use a classic for loop when you control the counter, and range when you visit every item.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Classic for<\/th><th>range<\/th><\/tr><\/thead><tbody><tr><td>Counter control<\/td><td>Full: start, stop and step<\/td><td>Fixed: every item in order<\/td><\/tr><tr><td>Works on<\/td><td>Any condition<\/td><td>Slices, arrays, strings, maps, ints, channels<\/td><\/tr><tr><td>Skip or step by 2<\/td><td>Easy, i += 2<\/td><td>Needs continue<\/td><\/tr><tr><td>Gives value copy<\/td><td>You read items[i]<\/td><td>Yes, v is a copy<\/td><\/tr><tr><td>Best for<\/td><td>Counting, reverse loops<\/td><td>Visiting every element<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use range to visit every item, and a classic for loop when you need a custom step or direction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Loop Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Off-by-one errors:<\/strong> <code>i &lt; n<\/code> and <code>i &lt;= n<\/code> differ by one pass, so check both ends.<\/li>\n\n\n<li><strong>Changing the value in range:<\/strong> the value is a copy, so <code>value = 0<\/code> does not change the slice. Use <code>items[i] = 0<\/code>.<\/li>\n\n\n<li><strong>Expecting map order:<\/strong> map order is random, so sort the keys when order matters.<\/li>\n\n\n<li><strong>break inside switch:<\/strong> a <code>break<\/code> inside a <code>switch<\/code> only leaves the switch, not the loop.<\/li>\n\n\n<li><strong>Endless loops by accident:<\/strong> a while-style loop needs something inside that moves it toward the end.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Loop Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Prefer range<\/strong> for slices and maps, because it avoids index mistakes.<\/li>\n\n\n<li><strong>Use range over int<\/strong> for simple counts, which is cleaner since Go 1.22.<\/li>\n\n\n<li><strong>Keep loop bodies short<\/strong> and move big blocks into functions.<\/li>\n\n\n<li><strong>Use labels for nested breaks<\/strong>, so the code says exactly which loop it leaves.<\/li>\n\n\n<li><strong>Test the edges<\/strong>, such as zero items, one item and the last item.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Loop Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go loop programs to test both logic and loop rules. Every program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax Questions on Go Loop Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. Does Go have a while loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Go has only <code>for<\/code>, but <code>for condition { }<\/code> works exactly like a while loop. Also, <code>for { }<\/code> with no condition is an endless loop.<\/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    number := 10\n    for number &gt; 0 { \/\/ while-style loop\n        fmt.Print(number, \" \")\n        number -= 3 \/\/ same as number = number - 3\n    }\n    fmt.Println(\"| end:\", number)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>10 7 4 1 | end: -2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop runs while <code>number<\/code> is above 0. After printing 1, <code>number<\/code> becomes -2, so the condition fails and the loop stops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. 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    \/\/ i starts at 0 and goes up by 2; j starts at 10 and goes down by 2\n    for i, j := 0, 10; i &lt; j; i, j = i+2, j-2 {\n        fmt.Println(i, j)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 10\n2 8\n4 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for<\/code> loop can use two variables at once. Both move on each pass, and the loop stops when they meet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loop Variable Questions in Go Loop Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print in Go 1.22 and later?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A slice that holds small functions to run later\n    var actions []func()\n\n    for i := 0; i &lt; 3; i++ {\n        \/\/ func() { ... } is a function with no name (a closure); it remembers i\n        actions = append(actions, func() { fmt.Print(i, \" \") }) \/\/ append adds to the slice\n    }\n\n    \/\/ Now run each saved function\n    for _, action := range actions {\n        action()\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since Go 1.22, each pass of the loop gets its own copy of <code>i<\/code>, as long as your <code>go.mod<\/code> says <code>go 1.22<\/code> or later. So every closure remembers a different value. Before Go 1.22, this printed &#8220;3 3 3&#8221;, which was a famous bug.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. Does changing the value inside range change the slice?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 2, 3}\n\n    \/\/ value is a COPY of each item\n    for _, value := range numbers {\n        value *= 10 \/\/ changes only the copy\n    }\n    fmt.Println(numbers)\n\n    \/\/ numbers[i] is the real item in the slice\n    for i := range numbers {\n        numbers[i] *= 10\n    }\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3]\n[10 20 30]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>value<\/code> is a copy of each item, so changing it does nothing to the slice. To change the slice, write to <code>numbers[i]<\/code>.<\/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 \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 2, 3}\n    for index, value := range numbers {\n        if index == 0 {\n            numbers = append(numbers, 99) \/\/ add one more item while looping\n        }\n        fmt.Print(value, \" \")\n    }\n    fmt.Println(len(numbers)) \/\/ len gives the number of items\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range<\/code> reads the length once, when the loop starts. So the new item, 99, is not visited, even though the slice now has 4 items.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">break and continue Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. 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    for i := 1; i &lt;= 3; i++ {\n        switch i {\n        case 2:\n            break \/\/ this leaves only the switch\n        }\n        fmt.Print(i, \" \")\n    }\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>break<\/code> only leaves the <code>switch<\/code>, not the loop. So all three numbers still print. Use a label if you want to leave the loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. How do you break out of two loops at once?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\nouter: \/\/ a label gives the outer loop a name\n    for i := 1; i &lt;= 3; i++ {\n        for j := 1; j &lt;= 3; j++ {\n            if i*j == 4 {\n                break outer \/\/ leave BOTH loops\n            }\n            fmt.Print(i, \"x\", j, \" \")\n        }\n    }\n    fmt.Println(\"done\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1x1 1x2 1x3 2x1 done<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The label <code>outer<\/code> names the outer loop. <code>break outer<\/code> leaves both loops when 2 x 2 equals 4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does continue with a label do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It skips to the next pass of the labelled loop. For example, <code>continue outer<\/code> stops the inner loop and moves the outer loop forward by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. 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    for i := 0; i &lt; 3; i++ {\n        defer fmt.Print(i, \" \") \/\/ defer waits and runs this when main ends\n    }\n    fmt.Print(\"start \")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>start 2 1 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Deferred calls run when <code>main<\/code> ends, in reverse order. So the loop queues 0, 1 and 2, and they print as 2, 1, 0 after &#8220;start&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. 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    word := \"h\u00e9llo\"\n    fmt.Println(len(word)) \/\/ len counts bytes\n\n    count := 0\n    for range word { \/\/ range counts characters\n        count++\n    }\n    fmt.Println(count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>6\n5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>len<\/code> counts bytes, and &#8220;\u00e9&#8221; takes 2 bytes. But <code>range<\/code> counts characters, so the loop runs 5 times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. 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    sum := 0\n    for i := 1; i &lt;= 10; i++ {\n        if i%3 != 0 {\n            continue \/\/ skip numbers that do not divide by 3\n        }\n        sum += i\n    }\n    fmt.Println(sum)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>18<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop only adds numbers that divide by 3, which are 3, 6 and 9. Their sum is 18.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13. 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    number := 12345\n    sum := 0\n    for number != 0 {\n        lastDigit := number % 10\n        sum += lastDigit\n        number \/= 10 \/\/ remove the last digit\n    }\n    fmt.Println(sum)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>15<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is the sum of digits program. It adds 5, 4, 3, 2 and 1, which gives 15.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14. 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    var numbers []int \/\/ an empty (nil) slice\n    for index, value := range numbers {\n        fmt.Println(index, value)\n    }\n    fmt.Println(\"empty loop ok\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>empty loop ok<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ranging over a nil or empty slice is safe. The body simply never runs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15. 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    values := [3]int{1, 2, 3} \/\/ [3]int is an array with a fixed size of 3\n    for index, value := range values {\n        values[2] = 100\n        if index == 2 {\n            fmt.Println(value)\n        }\n    }\n    fmt.Println(values[2])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3\n100<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ranging over an array works on a copy of the whole array. So <code>value<\/code> still reads the old 3, even though <code>values[2]<\/code> became 100. With a slice, <code>value<\/code> would be 100.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16. How do you loop backwards?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start at the end and count down, as in <code>for i := len(items) - 1; i &gt;= 0; i-- { }<\/code>. Since Go 1.23, you can also write <code>for i, item := range slices.Backward(items)<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17. When is an endless for loop useful?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is used for servers, game loops and menus that run until the user quits. You leave it with <code>break<\/code>, <code>return<\/code> or by stopping the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">FizzBuzz is one of the most famous Go loop programs in interviews. Write a program that prints all numbers from 1 to 50, but prints &#8220;Fizz&#8221; for multiples of 3, &#8220;Buzz&#8221; for multiples of 5 and &#8220;FizzBuzz&#8221; for multiples of both. Hint: check the &#8220;both&#8221; case first.<\/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\/flowcontrol\/1\">A Tour of Go: For<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#For_statements\">Go spec: for statements<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/loopvar-preview\">Fixing for loops in Go 1.22 (Go blog)<\/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 loop programs use only <code>for<\/code>, in classic, while, endless and range forms.<\/li>\n\n\n<li>Since Go 1.22, you can range over an int, and each loop pass gets its own variable.<\/li>\n\n\n<li><code>break<\/code> leaves a loop, <code>continue<\/code> skips a pass, and labels control nested loops.<\/li>\n\n\n<li><code>range<\/code> gives a copy of each value, so write to <code>items[i]<\/code> to change a slice.<\/li>\n\n\n<li>Map order is random, so sort keys with <code>slices.Sorted(maps.Keys(stock))<\/code> when order matters.<\/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, we use loops to test numbers. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-number-programs\/\">Go number programs with output<\/a>, where we check prime, palindrome and Armstrong numbers. For more on functions used inside loops, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-functions-beginners-guide\/\">Go functions guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go loop programs let your code repeat work, such as printing a times table, adding up numbers or walking through a list. In this&hellip;<\/p>\n","protected":false},"author":1,"featured_media":722,"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,27,95,11,104],"swapz_series":[],"class_list":["post-739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-for-loops","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 Loop Programs: Table, Factorial and Fibonacci<\/title>\n<meta name=\"description\" content=\"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.\" \/>\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-loop-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Loop Programs: Table, Factorial and Fibonacci\" \/>\n<meta property=\"og:description\" content=\"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-loop-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-04T11:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-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-loop-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Loop Programs with Output: Table, Factorial, Fibonacci\",\"datePublished\":\"2026-10-04T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/\"},\"wordCount\":2063,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-loop-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go For Loops\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/\",\"name\":\"Go Loop Programs: Table, Factorial and Fibonacci\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-loop-programs-featured.png\",\"datePublished\":\"2026-10-04T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:42+00:00\",\"description\":\"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-loop-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-loop-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Loop Programs featured image: output of table, factorial and Fibonacci loops\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-loop-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Loop Programs with Output: Table, Factorial, Fibonacci\"}]},{\"@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 Loop Programs: Table, Factorial and Fibonacci","description":"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.","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-loop-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Loop Programs: Table, Factorial and Fibonacci","og_description":"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.","og_url":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-04T11:30:00+00:00","article_modified_time":"2026-11-28T19:59:42+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-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-loop-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Loop Programs with Output: Table, Factorial, Fibonacci","datePublished":"2026-10-04T11:30:00+00:00","dateModified":"2026-11-28T19:59:42+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/"},"wordCount":2063,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-featured.png","keywords":["Beginner","Go For Loops","Go Interview Questions","Go Lang","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/","name":"Go Loop Programs: Table, Factorial and Fibonacci","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-featured.png","datePublished":"2026-10-04T11:30:00+00:00","dateModified":"2026-11-28T19:59:42+00:00","description":"Go loop programs with output: tables, factorial, Fibonacci, reverse a number, range and nested loops, plus loop interview questions with answers.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-loop-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-loop-programs-featured.png","width":2400,"height":1260,"caption":"Go Loop Programs featured image: output of table, factorial and Fibonacci loops"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-loop-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Loop Programs with Output: Table, Factorial, Fibonacci"}]},{"@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\/739","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=739"}],"version-history":[{"count":2,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/739\/revisions\/873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/722"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=739"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}