Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Go Recursion Functions
Go Recursion Functions
Go recursion functions को accept करता है। एक function recursive होता है अगर वह स्वयं को call करता है और एक stop condition तक पहुँचता है।
Recursion Example 1
testcount() function खुद को call करता है। x variable हर recursion में 1 से increment होता है। Recursion तब समाप्त होती है जब x == 11 होता है:
package main
import ("fmt")
func testcount(x int) int {
if x == 11 {
return 0
}
fmt.Println(x)
return testcount(x + 1)
}
func main(){
testcount(1)
}
Result:
1 2 3 4 5 6 7 8 9 10
Recursion एक common mathematical और programming concept है। इसका उपयोग data को loop करके desired result तक पहुँचने के लिए किया जाता है।
Developer को recursion function लिखते समय ध्यान रखना चाहिए क्योंकि:
- गलत लिखने पर function कभी terminate नहीं होगा।
- Excess memory या processor use कर सकता है।
- सही तरीके से लिखा जाए तो recursion efficient और mathematically elegant solution होता है।
Recursion Example 2: Factorial
factorial_recursion() function खुद को call करता है। x variable हर recursion में decrement होता है। Recursion तब समाप्त होती है जब x <= 0 होता है:
package main
import ("fmt")
func factorial_recursion(x float64) (y float64) {
if x > 0 {
y = x * factorial_recursion(x-1)
} else {
y = 1
}
return
}
func main() {
fmt.Println(factorial_recursion(4))
}
Result:
24
New developers के लिए यह समझना थोड़ा time ले सकता है कि recursion exactly कैसे काम करती है। सबसे अच्छा तरीका है testing और modifying करना।