SoFunction
Updated on 2025-03-03

In-depth analysis of recursive use in Go programming

Recursion is the process of repeating the project in a similar way. The same applies to programming languages. If a program can let you call the function in which the same function is called, the recursive call function is used as follows.

Copy the codeThe code is as follows:

func recursion() {
   recursion() /* function calls itself */
}

func main() {
   recursion()
}


The Go programming language supports recursion, that is, the function to be called itself. However, when using recursion, programmers need to be careful to determine the exit conditions of the function, otherwise it will cause infinite loops.

Recursive functions are very useful to solve many mathematical problems. If you want to calculate a factorial, you can generate Fibon series, etc.

Number Multiplication
Here is an example that calculates factorials for using a recursive function by a given number:

Copy the codeThe code is as follows:

package main

import "fmt"

func factorial(i int) {
   if(i <= 1) {
      return 1
   }
   return i * factorial(i - 1)
}

func main { 
    var i int = 15
    ("Factorial of %d is %d\n", i, factorial(i))
}


Let's compile and run the above program, which will produce the following results:
Copy the codeThe code is as follows:

Factorial of 15 is 2004310016

Fibonacci series
Here is another example that produces Fibon concatenation using a recursive function given a number:
Copy the codeThe code is as follows:

package main

import "fmt"

func fibonaci(i int) {
   if(i == 0) {
      return 0
   }
   if(i == 1) {
      return 1
   }
   return fibonaci(i-1) + fibonaci(i-2)
}

func main() {
    var i int
    for i = 0; i < 10; i++ {
       ("%d\t%n", fibonaci(i))
    }   
}


Let's compile and run the above program, which will produce the following results:
0 1 1 2 3 5 8 13 21 34

golang recursively judges the back text string
Judging palindromic strings is a classic problem.

The idea is to compare the first character with the most character. If you do not wait for exit, continue the process just now until the first character and the last character meet or their distance is 1. It means they are palindrome strings.

The following code will ignore whitespace characters, such as "1  1  2 1" will make it a palindrome string.

Copy the codeThe code is as follows:

package main

import (
    "fmt"
    "os"
    "strings"
    "unicode/utf8"
)

func doPalindrome(s string) bool {
    if (s) <= 1 {
        return true
    }  

    word := (s, "\t \r\n\v")
    first, sizeOfFirst := (word)
    last, sizeOfLast := (word)

    if first != last {
        return false
    }  
    return doPalindrome(word[sizeOfFirst : len(word)-sizeOfLast])
}

func IsPalindrome(word string) bool {
    s := ""
    s = (word, "\t \r\n\v")
    if len(s) == 0 || len(s) == 1 {
        return false
    }  
    return doPalindrome(s)
}

func main() {
    args := [1:]
    for _, v := range args {
        ok := IsPalindrome(v)
        if ok {
            ("%s\n", v)
        }  
    }  

}