Go by Example - Recursion

翻译自 https://gobyexample.com/

Go by Example

Go is an open source programming language designed for building simple, fast, and reliable software.

Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.

Go by Example: Recursion

golang也支持递归函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import "fmt"

// 定义一个函数fact递归调用自己直到参数变为0。
func fact(n int) int {
if n == 0 {
return 1
}

return n * fact(n-1)
}

func main() {
fmt.Println(fact(7))
}

原文链接:Go by Example: Recursion