{"id":805,"date":"2026-10-07T13:00:00","date_gmt":"2026-10-07T07:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/"},"modified":"2026-11-29T01:29:34","modified_gmt":"2026-11-28T19:59:34","slug":"go-recursion-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/","title":{"rendered":"Go Recursion Programs: Factorial, Fibonacci, Hanoi and More"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go recursion programs solve a problem by having a function call itself on a smaller version of the same problem. Recursion makes some tasks, like Tower of Hanoi, tree walks and subsets, much shorter than loops. In this post, you will write fourteen recursion programs with their exact output, and then practice the recursion questions interviewers use to test your thinking.<\/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>, since recursion is just a function calling itself. We also redo some loops from our <a href=\"https:\/\/smitabharat.com\/blog\/go-loop-programs\/\">Go loop programs<\/a> the recursive way, so you can compare both styles. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Recursion Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go recursion programs use a function that calls itself. Every recursive function needs two parts: a <strong>base case<\/strong> that stops the calls, and a <strong>recursive case<\/strong> that moves closer to the base case each time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a set of Russian dolls. To reach the smallest doll, you open one doll, then open the doll inside it, and so on. You stop when a doll does not open. That last doll is the base case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Recursion Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some problems have a shape that repeats inside itself. For example, a folder contains folders, a tree has smaller trees and a maths formula like factorial is defined using itself. Go recursion programs let you write these problems almost exactly as you would describe them.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Trees and folders:<\/strong> walking nested data is natural with recursion.<\/li>\n\n\n<li><strong>Divide and conquer:<\/strong> merge sort and quick sort are recursive.<\/li>\n\n\n<li><strong>Interviews:<\/strong> factorial, Fibonacci, Hanoi and subsets are classic questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Recursion Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each call gets its own copy of the parameters and local variables on the call stack. The calls stack up until one hits the base case. Then the results flow back down, one call at a time, until the first call returns the final answer.<\/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-recursion-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-1024x640.png\" alt=\"Go recursion programs diagram showing factorial calls stacking up and returning\" class=\"wp-image-798\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Each call waits for a smaller call, the base case returns 1, and the results multiply back down to the first call.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the base case and recursive case for each program in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Problem<\/th><th>Base case<\/th><th>Recursive case<\/th><\/tr><\/thead><tbody><tr><td>Factorial<\/td><td>n &lt;= 1 returns 1<\/td><td>n * fact(n-1)<\/td><\/tr><tr><td>Fibonacci<\/td><td>n &lt; 2 returns n<\/td><td>fib(n-1) + fib(n-2)<\/td><\/tr><tr><td>Power<\/td><td>exp == 0 returns 1<\/td><td>base * pow(base, exp-1)<\/td><\/tr><tr><td>GCD<\/td><td>b == 0 returns a<\/td><td>gcd(b, a % b)<\/td><\/tr><tr><td>Sum of digits<\/td><td>n == 0 returns 0<\/td><td>n%10 + sum(n\/10)<\/td><\/tr><tr><td>Reverse string<\/td><td>len 0 or 1<\/td><td>reverse(rest) + first<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Classic Go Recursion Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go recursion programs are the ones every interviewer expects you to know. If recursion is new to you, start with the two easy warm-ups in Program 1 and Program 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Say Hello a Few Times<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the smallest recursive function you can write. It prints a message, then calls itself with one less to go.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ sayHello prints \"Hello\" the given number of times, using recursion.\nfunc sayHello(times int) {\n    \/\/ Base case: nothing left to print, so stop.\n    if times == 0 {\n        return\n    }\n    fmt.Println(\"Hello, times left:\", times)\n    \/\/ Recursive case: call itself with a smaller number.\n    sayHello(times - 1)\n}\n\nfunc main() {\n    sayHello(3)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, times left: 3\nHello, times left: 2\nHello, times left: 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first call gets 3, prints a line and then calls <code>sayHello(2)<\/code>. Each new call gets a smaller number. When the number reaches 0, the base case returns and the calls stop. Without that <code>if<\/code>, the function would call itself forever.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Add the Numbers from 1 to N<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ sumUpTo returns 1 + 2 + ... + number.\nfunc sumUpTo(number int) int {\n    \/\/ Base case: the sum up to 0 is 0.\n    if number == 0 {\n        return 0\n    }\n    \/\/ Recursive case: this number plus the sum of all smaller numbers.\n    return number + sumUpTo(number-1)\n}\n\nfunc main() {\n    fmt.Println(sumUpTo(5))\n    fmt.Println(sumUpTo(10))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n55<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sumUpTo(5)<\/code> asks for 5 plus <code>sumUpTo(4)<\/code>, which asks for 4 plus <code>sumUpTo(3)<\/code>, and so on. At 0, the base case gives back 0. Then each waiting call adds its own number, so you get 1 + 2 + 3 + 4 + 5 = 15.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Factorial<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ factorial returns number x (number-1) x ... x 1.\nfunc factorial(number int) int {\n    if number &lt;= 1 {\n        return 1 \/\/ base case\n    }\n    return number * factorial(number-1) \/\/ recursive case\n}\n\nfunc main() {\n    for _, number := range []int{0, 1, 5, 10} { \/\/ range gives each index (ignored with _) and value\n        fmt.Printf(\"%d! = %d\\n\", number, factorial(number)) \/\/ %d prints a whole number\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0! = 1\n1! = 1\n5! = 120\n10! = 3628800<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This has the same shape as Program 2, but it multiplies instead of adds. <code>factorial(5)<\/code> becomes 5 x <code>factorial(4)<\/code>, and so on, until <code>factorial(1)<\/code> returns 1. Then the multiplications finish on the way back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Fibonacci, Plain and Memoized<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The plain version is short but slow, because it repeats the same work many times. A memo map stores answers we already know.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ fib returns the Fibonacci number at the given position, the slow way.\nfunc fib(position int) int {\n    if position &lt; 2 {\n        return position \/\/ base case: fib(0) is 0, fib(1) is 1\n    }\n    return fib(position-1) + fib(position-2)\n}\n\n\/\/ fibMemo does the same, but saves each answer in memo so it is worked out once.\nfunc fibMemo(position int, memo map[int]int) int {\n    if position &lt; 2 {\n        return position\n    }\n    \/\/ Step 1: if we already know the answer, return it. ok is true if the key was found.\n    value, ok := memo[position]\n    if ok {\n        return value\n    }\n    \/\/ Step 2: work it out once and save it in the map.\n    memo[position] = fibMemo(position-1, memo) + fibMemo(position-2, memo)\n    return memo[position]\n}\n\nfunc main() {\n    fmt.Println(fib(10))\n    fmt.Println(fibMemo(80, map[int]int{})) \/\/ map[int]int{} is a new empty map\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>55\n23416728348467685<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both functions use the rule &#8220;add the two numbers before&#8221;. The memo version checks the map first and only does the work for new positions. Plain <code>fib(80)<\/code> would take a very long time, because it makes billions of calls. With memoization, each number is worked out once, so <code>fibMemo(80)<\/code> is instant.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Power with Fast Exponent<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ power returns base multiplied by itself exponent times.\nfunc power(base, exponent int) int {\n    \/\/ Base case: anything to the power 0 is 1.\n    if exponent == 0 {\n        return 1\n    }\n    \/\/ Step 1: work out the answer for half the exponent.\n    half := power(base, exponent\/2)\n    \/\/ Step 2: square it. If the exponent is odd, multiply by base once more.\n    if exponent%2 == 0 { \/\/ % gives the remainder, so 0 means even\n        return half * half\n    }\n    return half * half * base\n}\n\nfunc main() {\n    fmt.Println(power(2, 10), power(3, 5), power(7, 0))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1024 243 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The idea is that 2 to the 10 equals (2 to the 5) times (2 to the 5). So each call halves the exponent instead of taking one step down. As a result, <code>power(2, 10)<\/code> needs only about 4 calls, not 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: GCD with Euclid&#8217;s Method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ gcd returns the greatest common divisor of first and second.\nfunc gcd(first, second int) int {\n    \/\/ Base case: when second is 0, first is the answer.\n    if second == 0 {\n        return first\n    }\n    \/\/ Recursive case: the GCD stays the same if we use second and the remainder.\n    return gcd(second, first%second)\n}\n\nfunc main() {\n    fmt.Println(gcd(48, 180), gcd(17, 5), gcd(100, 75))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>12 1 25<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call swaps in the remainder, so the numbers shrink fast. When the remainder becomes 0, the other number is the GCD. This is the recursive form of the loop in our <a href=\"https:\/\/smitabharat.com\/blog\/go-number-programs\/\">Go number programs<\/a>. Both give the same answers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Digit and String Go Recursion Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go recursion programs break numbers and strings into smaller pieces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Sum of Digits<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ digitSum adds up all the digits of number.\nfunc digitSum(number int) int {\n    if number == 0 {\n        return 0 \/\/ base case: no digits left\n    }\n    lastDigit := number % 10 \/\/ 98765 % 10 is 5\n    rest := number \/ 10      \/\/ 98765 \/ 10 is 9876\n    return lastDigit + digitSum(rest)\n}\n\nfunc main() {\n    fmt.Println(digitSum(98765), digitSum(7), digitSum(0))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>35 7 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>% 10<\/code> gives the last digit, and <code>\/ 10<\/code> drops it. Each call keeps one digit and passes the rest on. When nothing is left, the base case returns 0, and the digits add up on the way back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Reverse a String<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ reverse returns the letters in the opposite order.\nfunc reverse(letters []rune) string { \/\/ a rune is one character\n    \/\/ Base case: zero or one letter is already reversed.\n    if len(letters) &lt;= 1 {\n        return string(letters)\n    }\n    \/\/ Reverse everything after the first letter, then put the first letter last.\n    \/\/ letters[1:] means \"from index 1 to the end\".\n    return reverse(letters[1:]) + string(letters[0])\n}\n\nfunc main() {\n    fmt.Println(reverse([]rune(\"golang\"))) \/\/ []rune(...) splits a string into characters\n    fmt.Println(reverse([]rune(\"\u091a\u093e\u092f\")))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>gnalog\n\u092f\u093e\u091a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We take the first rune off, reverse the rest, and add the first rune at the end. Working with runes keeps each letter whole, though Hindi vowel signs may still look odd once moved.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Palindrome Check<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ isPalindrome reports whether word reads the same forwards and backwards.\nfunc isPalindrome(word string) bool {\n    \/\/ Base case: zero or one letter is always a palindrome.\n    if len(word) &lt;= 1 {\n        return true\n    }\n    \/\/ Step 1: the first and last letters must match.\n    if word[0] != word[len(word)-1] {\n        return false\n    }\n    \/\/ Step 2: check the middle part, without the first and last letters.\n    return isPalindrome(word[1 : len(word)-1])\n}\n\nfunc main() {\n    for _, word := range []string{\"racecar\", \"level\", \"golang\", \"a\"} {\n        fmt.Println(word, isPalindrome(word))\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>racecar true\nlevel true\ngolang false\na true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call compares the outer two letters and then checks the inside. For &#8220;racecar&#8221;, it checks r and r, then a and a, then c and c, and stops at &#8220;e&#8221;. This byte version is fine for plain English words.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Decimal to Binary<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"strconv\"\n)\n\n\/\/ toBinary turns a whole number into a string of 0s and 1s.\nfunc toBinary(number int) string {\n    \/\/ Base case: 0 and 1 are the same in binary.\n    if number &lt; 2 {\n        return strconv.Itoa(number) \/\/ Itoa turns an int into a string\n    }\n    \/\/ The higher bits come from number\/2, and the last bit is number%2.\n    return toBinary(number\/2) + strconv.Itoa(number%2)\n}\n\nfunc main() {\n    fmt.Println(toBinary(13), toBinary(45), toBinary(0))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1101 101101 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dividing by 2 drops the last binary digit, and <code>% 2<\/code> tells you what that digit was. The recursive call builds the higher bits first, so the digits come out in the right order without reversing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced Go Recursion Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go recursion programs solve problems that are hard to write with a simple loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Tower of Hanoi<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Move all disks from peg A to peg C, one at a time, never putting a bigger disk on a smaller one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ moves counts every move. It sits outside the function so all calls share it.\nvar moves int\n\n\/\/ hanoi moves the top \"disks\" disks from one peg to another, using a spare peg.\nfunc hanoi(disks int, from, to, spare string) {\n    if disks == 0 {\n        return \/\/ base case: no disks to move\n    }\n    \/\/ Step 1: move the smaller disks out of the way, onto the spare peg.\n    hanoi(disks-1, from, spare, to)\n    \/\/ Step 2: move the biggest disk of this group to its target.\n    moves++\n    fmt.Printf(\"Move disk %d from %s to %s\\n\", disks, from, to)\n    \/\/ Step 3: move the smaller disks back on top of it.\n    hanoi(disks-1, spare, to, from)\n}\n\nfunc main() {\n    hanoi(3, \"A\", \"C\", \"B\")\n    fmt.Println(\"Total moves:\", moves)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Move disk 1 from A to C\nMove disk 2 from A to B\nMove disk 1 from C to B\nMove disk 3 from A to C\nMove disk 1 from B to A\nMove disk 2 from B to C\nMove disk 1 from A to C\nTotal moves: 7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To move 3 disks, move the top 2 out of the way, move the big disk, then move the 2 back on top. Moving 2 disks uses the same three steps with 1 disk. The total is always 2 to the power n, minus 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Sum of a Slice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ sum adds up all the numbers in the slice.\nfunc sum(numbers []int) int {\n    if len(numbers) == 0 {\n        return 0 \/\/ base case: an empty slice adds up to 0\n    }\n    \/\/ The first number plus the sum of the rest.\n    return numbers[0] + sum(numbers[1:])\n}\n\nfunc main() {\n    fmt.Println(sum([]int{4, 8, 15, 16, 23, 42}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>108<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call takes the first number and passes the rest of the slice on. The slice gets one item shorter on every call. When it is empty, the base case returns 0 and the numbers add up.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: All Subsets (Power Set)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ subsets builds every subset of items and saves each one into results.\nfunc subsets(items []string, index int, current []string, results *[][]string) {\n    \/\/ Base case: every item has been decided, so save a copy of current.\n    if index == len(items) {\n        saved := append([]string{}, current...) \/\/ copy current into a new slice\n        *results = append(*results, saved)      \/\/ *results is the slice the pointer points to\n        return\n    }\n    \/\/ Choice 1: skip this item.\n    subsets(items, index+1, current, results)\n    \/\/ Choice 2: take this item.\n    withItem := append(current, items[index])\n    subsets(items, index+1, withItem, results)\n}\n\nfunc main() {\n    var result [][]string\n    subsets([]string{\"a\", \"b\", \"c\"}, 0, nil, &amp;result) \/\/ &amp;result passes the address, so subsets can add to it\n    fmt.Println(len(result), result)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>8 [[] [c] [b] [b c] [a] [a c] [a b] [a b c]]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For each item, we make two choices: skip it or take it. With 3 items, that gives 2 x 2 x 2 = 8 subsets. We copy <code>current<\/code> before saving it, so later changes do not affect saved results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Walk a Nested Menu (Tree)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"strings\"\n)\n\n\/\/ Menu is one menu item that can hold smaller menu items inside it.\ntype Menu struct {\n    Name     string\n    Children []Menu\n}\n\n\/\/ printMenu prints a menu and all its children, indented by depth.\nfunc printMenu(menu Menu, depth int) {\n    \/\/ strings.Repeat adds two spaces for each level of depth.\n    fmt.Println(strings.Repeat(\"  \", depth) + \"- \" + menu.Name)\n    for _, child := range menu.Children {\n        printMenu(child, depth+1) \/\/ each child is printed one level deeper\n    }\n}\n\nfunc main() {\n    root := Menu{\"Shop\", []Menu{\n        {\"Men\", []Menu{{\"Shirts\", nil}, {\"Shoes\", nil}}}, \/\/ nil means \"no children\"\n        {\"Women\", []Menu{{\"Dresses\", nil}}},\n        {\"Sale\", nil},\n    }}\n    printMenu(root, 0)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>- Shop\n  - Men\n    - Shirts\n    - Shoes\n  - Women\n    - Dresses\n  - Sale<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function prints its own name, then calls itself for every child with a bigger indent. There is no explicit base case here. The recursion stops naturally when a menu has no children, because the loop has nothing to visit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recursion vs Loops in Go Recursion Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most Go recursion programs can also be written with a loop. The diagram and table below compare the two.<\/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-recursion-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-1024x640.png\" alt=\"Go recursion programs comparison of recursion and loops\" class=\"wp-image-799\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Recursion is shorter for trees and divide and conquer, while loops use less memory and are often faster.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Recursion<\/th><th>Loop<\/th><\/tr><\/thead><tbody><tr><td>Code for trees and nested data<\/td><td>Short and clear<\/td><td>Needs your own stack<\/td><\/tr><tr><td>Memory<\/td><td>One stack frame per call<\/td><td>Constant<\/td><\/tr><tr><td>Speed<\/td><td>Slower, call overhead<\/td><td>Faster<\/td><\/tr><tr><td>Risk<\/td><td>Stack overflow if too deep<\/td><td>Endless loop if wrong<\/td><\/tr><tr><td>Best for<\/td><td>Trees, Hanoi, subsets, sorts<\/td><td>Counting, sums, simple sequences<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So use recursion when the problem is naturally nested. Use a loop for simple counting, because it is faster and uses less memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Recursion Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>No base case:<\/strong> the function calls itself forever and crashes with a stack overflow.<\/li>\n\n\n<li><strong>Not moving toward the base case:<\/strong> <code>fact(n)<\/code> calling <code>fact(n)<\/code> never stops.<\/li>\n\n\n<li><strong>Plain recursive Fibonacci for big n:<\/strong> it repeats work, so add a memo or use a loop.<\/li>\n\n\n<li><strong>Sharing a slice in results:<\/strong> in subset problems, copy the current slice before saving it.<\/li>\n\n\n<li><strong>Expecting tail call optimisation:<\/strong> Go does not do it, so very deep recursion still uses stack.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Recursion Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Write the base case first<\/strong>, before the recursive call.<\/li>\n\n\n<li><strong>Make each call smaller<\/strong>, so the base case is always reached.<\/li>\n\n\n<li><strong>Memoize repeated sub-problems<\/strong> with a map.<\/li>\n\n\n<li><strong>Prefer a loop<\/strong> when the recursion is just a straight line, like a sum.<\/li>\n\n\n<li><strong>Test the smallest inputs<\/strong>, such as 0, 1 and an empty slice.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Recursion Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go recursion programs to test how recursion behaves. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go Recursion Programs Concept Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What are the two parts of every recursive function?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A base case that returns without calling itself, and a recursive case that calls itself with a smaller input. Without a base case, the function never stops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. How many calls does plain fib(10) make?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ calls counts how many times fib runs.\nvar calls int\n\n\/\/ fib returns the Fibonacci number at position and counts every call.\nfunc fib(position int) int {\n    calls++\n    if position &lt; 2 {\n        return position\n    }\n    return fib(position-1) + fib(position-2)\n}\n\nfunc main() {\n    fmt.Println(fib(10), calls)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>55 177<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">177 calls for such a small number shows how much work is repeated. The number of calls grows exponentially, which is why memoization matters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What happens when recursion never stops in Go?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The goroutine stack keeps growing until it hits Go&#8217;s limit, which is about 1 GB on 64-bit systems. Then the program crashes with &#8220;fatal error: stack overflow&#8221;. Go stacks grow on demand, so you get much deeper than in many languages before this happens.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. Does Go optimise tail recursion?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. A tail call still adds a new stack frame. So for very deep problems, rewrite the recursion as a loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Output Questions<\/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 \"fmt\"\n\n\/\/ countDown prints number, number-1, ... 1 and then \"Go!\".\nfunc countDown(number int) {\n    if number == 0 {\n        fmt.Println(\"Go!\")\n        return\n    }\n    fmt.Print(number, \" \") \/\/ print first, then call\n    countDown(number - 1)\n}\n\nfunc main() {\n    countDown(5)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>5 4 3 2 1 Go!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call prints its number before it calls itself. So the numbers come out from 5 down to 1, and the base case prints &#8220;Go!&#8221;.<\/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\n\/\/ show prints the numbers from 1 up to number.\nfunc show(number int) {\n    if number == 0 {\n        return\n    }\n    show(number - 1)        \/\/ call first\n    fmt.Print(number, \" \") \/\/ print after the smaller call has finished\n}\n\nfunc main() {\n    show(4)\n    fmt.Println()\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\">The print comes after the recursive call. So the deepest call prints first, and the numbers come out in increasing order. Moving the print before the call would reverse the order.<\/p>\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\n\/\/ isEven says number is even if number-1 is odd.\nfunc isEven(number int) bool {\n    if number == 0 {\n        return true\n    }\n    return isOdd(number - 1)\n}\n\n\/\/ isOdd says number is odd if number-1 is even.\nfunc isOdd(number int) bool {\n    if number == 0 {\n        return false\n    }\n    return isEven(number - 1)\n}\n\nfunc main() {\n    fmt.Println(isEven(10), isOdd(7), isEven(3))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true true false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is mutual recursion: two functions that call each other. Go allows it because functions in a package can call each other in any order.<\/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 \"fmt\"\n\n\/\/ count returns how many steps of 2 it takes to reach 0 or below.\nfunc count(number int) int {\n    if number &lt;= 0 {\n        return 0\n    }\n    return 1 + count(number-2)\n}\n\nfunc main() {\n    fmt.Println(count(7), count(8))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For 7, the calls go 7, 5, 3, 1, which is 4 calls before reaching zero or below. For 8, they go 8, 6, 4, 2, which is also 4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. How would you print a number&#8217;s digits in order with recursion?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ printDigits prints each digit of number from left to right.\nfunc printDigits(number int) {\n    if number &gt;= 10 {\n        printDigits(number \/ 10) \/\/ print the higher digits first\n    }\n    fmt.Print(number%10, \" \") \/\/ then print the last digit\n}\n\nfunc main() {\n    printDigits(4096)\n    fmt.Println()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4 0 9 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The recursive call handles the higher digits first, then the current call prints its own last digit.<\/p>\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\n\/\/ depth counts the items in levels, one per call.\nfunc depth(levels []string) int {\n    if len(levels) == 0 {\n        return 0\n    }\n    return 1 + depth(levels[1:]) \/\/ levels[1:] drops the first item\n}\n\nfunc main() {\n    fmt.Println(depth([]string{\"root\", \"dir\", \"subdir\", \"file\"}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call drops one item and adds 1. When the slice is empty, the base case returns 0, so the answer is the number of items.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. When should you choose recursion over a loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Choose recursion for naturally nested problems like trees, file systems, JSON documents and divide and conquer sorts. Choose a loop for straight sequences like sums, counts and simple series.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. What is memoization?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It means saving the result of each call in a map or slice, so the same input is never worked out twice. It turns plain recursive Fibonacci from exponential time into linear time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Staircase counting is one of the best-known Go recursion programs. Write a recursive function that counts how many ways you can climb a staircase of n steps if you can take 1 or 2 steps at a time. Print the answer for n = 1 to 10. Hint: the answer for n is the answer for n-1 plus the answer for n-2, so add a memo map.<\/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\/doc\/effective_go#functions\">Effective Go: functions<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Function_declarations\">Go spec: function declarations<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/faq#goroutines\">Go FAQ: why goroutines and stacks are cheap<\/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 recursion programs need a base case and a recursive case that moves toward it.<\/li>\n\n\n<li>Each call gets its own stack frame, and results return in reverse order.<\/li>\n\n\n<li>Memoization with a map stops plain Fibonacci from repeating work.<\/li>\n\n\n<li>Go does not optimise tail calls, so very deep recursion should become a loop.<\/li>\n\n\n<li>Recursion shines for trees, Hanoi, subsets and divide and conquer.<\/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 group related data together. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-struct-programs\/\">Go struct programs with output<\/a>, with employee records, methods and embedding. To revise sorting, which uses recursion in merge and quick sort, see our <a href=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\">Go sorting programs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go recursion programs solve a problem by having a function call itself on a smaller version of the same problem. Recursion makes some tasks,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":800,"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,95,11,104,117],"swapz_series":[],"class_list":["post-805","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-recursion"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Recursion Programs: Factorial, Fibonacci, Hanoi<\/title>\n<meta name=\"description\" content=\"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.\" \/>\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-recursion-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Recursion Programs: Factorial, Fibonacci, Hanoi\" \/>\n<meta property=\"og:description\" content=\"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-07T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-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=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Recursion Programs: Factorial, Fibonacci, Hanoi and More\",\"datePublished\":\"2026-10-07T07:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/\"},\"wordCount\":1976,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-recursion-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Recursion\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/\",\"name\":\"Go Recursion Programs: Factorial, Fibonacci, Hanoi\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-recursion-programs-featured.png\",\"datePublished\":\"2026-10-07T07:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:34+00:00\",\"description\":\"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-recursion-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-recursion-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Recursion Programs featured image: factorial, Fibonacci and Hanoi output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-recursion-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Recursion Programs: Factorial, Fibonacci, Hanoi and More\"}]},{\"@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 Recursion Programs: Factorial, Fibonacci, Hanoi","description":"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.","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-recursion-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Recursion Programs: Factorial, Fibonacci, Hanoi","og_description":"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-07T07:30:00+00:00","article_modified_time":"2026-11-28T19:59:34+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Recursion Programs: Factorial, Fibonacci, Hanoi and More","datePublished":"2026-10-07T07:30:00+00:00","dateModified":"2026-11-28T19:59:34+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/"},"wordCount":1976,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Recursion"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/","name":"Go Recursion Programs: Factorial, Fibonacci, Hanoi","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-featured.png","datePublished":"2026-10-07T07:30:00+00:00","dateModified":"2026-11-28T19:59:34+00:00","description":"Go recursion programs with output: factorial, Fibonacci with memo, power, GCD, Tower of Hanoi and subsets, plus recursion interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-recursion-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-recursion-programs-featured.png","width":2400,"height":1260,"caption":"Go Recursion Programs featured image: factorial, Fibonacci and Hanoi output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-recursion-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Recursion Programs: Factorial, Fibonacci, Hanoi and More"}]},{"@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\/805","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=805"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"predecessor-version":[{"id":886,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/805\/revisions\/886"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/800"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=805"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}