Пример 1. (я могу понять рекурсию в этом коде, функция findFactorial
вызывается внутри)
package main
import "fmt"
var repeated = 0
func main() {
fmt.Println("Answer :",findFactorial(3))
}
func findFactorial(x int) int {
repeated ++
fmt.Printf("Repeating.., Now repeated %v times\n",repeated)
if x == 0 {
return 1
}
return x * findFactorial(x-1) // it is evident that this same function is calling again
}
Вывод
go run .\factorial.go
Repeating.., Now repeated 1 times
Repeating.., Now repeated 2 times
Repeating.., Now repeated 3 times
Repeating.., Now repeated 4 times
Answer : 6
Пример 2. (Здесь я не могу понять, как происходит рекурсия)
package main
import (
"fmt"
"log"
"os"
"path/filepath"
)
var repeated = 0
func main() {
iterate("../../../../TEMP")
}
func iterate(path string) {
fmt.Println("path",path)
filepath.Walk(path, func(x string, info os.FileInfo, err error) error {
repeated ++
fmt.Printf("Repeating.., Now repeated %v times\n",repeated)
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("File Name: %s\n", info.Name())
return nil
})
}
Вывод
go run .\filepathwalktu.go
path ../../../../TEMP
Repeating.., Now repeated 1 times
File Name: TEMP
Repeating.., Now repeated 2 times
File Name: PNR.png
Repeating.., Now repeated 3 times
File Name: tkt.png
Repeating.., Now repeated 4 times
File Name: understand this.txt