Go - Recursion

Recursion is simply the system of repeating contraptions in a self-comparative way. The equivalent idea applies in programming dialects as pleasantly.

If a program allows to call a function inside the same function, then it is called a recursive function call.


func recursion() 

recursion()/* function  calls itself */ 

func principal() 


The Go programming language supports recursion. That is, it allows a function to call itself.

Examples of Recursion in Go

Recursive capacities are helpful to solve numerous scientific mathematical problems such as calculating factorial of a number creating a Fibonacci arrangement, and Fibonacci series.

Example 1: Calculating Factorial Using Recursion in Go

The accompanying example calculates the factorial of a given wide assortment utilizing a recursive function

bundle key 

import "fmt" 

func factorial(i int)int 

if(i <= 1) 

return 1 

return I * factorial(i - 1) 

func key() 

var I int = 15 

fmt.Printf("Factorial of %d will be %d", I, factorial(i)) 

Example 2: Fibonacci Series Using Recursion in Go


group principle 

import "fmt" 

func fibonaci(i int) (ret int) 

on the off chance that I == zero 

return zero 

on the off chance that I == 1 

return 1 

return fibonaci(i-1) + fibonaci(i-2) 

func central() 

var I int 

for I = zero; I < 20; i++ 

fmt.Printf("%d ", fibonaci(i)) 


$go run generally important.Cross 

0 1 2 3 5 eight 13 21 34 55 89 a hundred and forty four 233 377 610 987 1597 2584 4181