In depth analysis of the use of recursion in programming the Go language

  • 2020-05-30 20:20:20
  • OfStack

Recursion is the process of repeating items in a similar way. The same is true in programming languages, if a program allows you to call a function called by the same function, the recursive call function is used as follows.


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

The Go programming language supports recursion, that is, the function itself to be called. However, when using recursion, the programmer needs to be careful to determine the exit condition of the function, otherwise it will result in an infinite loop.

Recursive functions are very useful for solving many mathematical problems to calculate a number of factorials, producing fibre-wave series and so on

Digital factorial
Here is an example that calculates the factorial using a recursive function defined by a given number:


package main import "fmt" func factorial(i int) {
   if(i <= 1) {
      return 1
   }
   return i * factorial(i - 1)
} func main { 
    var i int = 15
    fmt.Printf("Factorial of %d is %d\n", i, factorial(i))
}

Let's compile and run the above program, which will produce the following results:

Factorial of 15 is 2004310016

Fibonacci series
The following is another example, which produces fibre-wave concatenation using 1 recursive function given 1 number:

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++ {
       fmt.Printf("%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 palindromes
Judging palindromic strings is a classic problem.

The idea is to take the first character and compare it to the first character, and if you don't wait to exit, continue the same process until the first character and the last character meet or their distance is 1. They are palindrome strings.

The following code ignores white space characters such as "1, 1, 2, 1" to make them palindromic strings.


package main import (
    "fmt"
    "os"
    "strings"
    "unicode/utf8"
) func doPalindrome(s string) bool {
    if utf8.RuneCountInString(s) <= 1 {
        return true
    }       word := strings.Trim(s, "\t \r\n\v")
    first, sizeOfFirst := utf8.DecodeRuneInString(word)
    last, sizeOfLast := utf8.DecodeLastRuneInString(word)     if first != last {
        return false
    }  
    return doPalindrome(word[sizeOfFirst : len(word)-sizeOfLast])
} func IsPalindrome(word string) bool {
    s := ""
    s = strings.Trim(word, "\t \r\n\v")
    if len(s) == 0 || len(s) == 1 {
        return false
    }  
    return doPalindrome(s)
} func main() {
    args := os.Args[1:]
    for _, v := range args {
        ok := IsPalindrome(v)
        if ok {
            fmt.Printf("%s\n", v)
        }  
    }   }


Related articles: