अनाम Functions
In this page:
func(param type) returnType {
// body
}
varName := func(param type) returnType {
// body
}
Defining and Calling an Anonymous Function
एक function literal एक नाम छोड़ देता है और इसे एक variable को assign किया जा सकता है, जिसे फिर उस variable name के बाद parentheses उपयोग करके बिल्कुल किसी regular function की तरह call किया जाता है।
उदाहरण: Defining and Calling an Anonymous Function
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create square and infer the type
square := func(n int) int {
// Send n * n back to the caller
return n * n
}
// Print the values followed by a newline
fmt.Println(square(7))
}
Login to try C/C++/Java/PHP code in the editor
Immediately Invoked Function Literals
किसी anonymous function को इसकी closing brace के ठीक बाद parentheses में arguments जोड़कर define होते ही call किया जा सकता है, एक-बार के logic के block को scope करने के लिए उपयोगी।
उदाहरण: Immediately Invoked Function Literals
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create result and infer the type
result := func(a, b int) int {
// Send a + b back to the caller
return a + b
}(3, 4)
// Print the values followed by a newline
fmt.Println(result)
}
Login to try C/C++/Java/PHP code in the editor
Anonymous Functions as Arguments
Anonymous functions अक्सर सीधे दूसरे functions को arguments के रूप में पास किए जाते हैं जो एक callback की उम्मीद करते हैं, सिर्फ एक जगह उपयोग होने वाले अलग function को declare और नाम देने से बचते हुए।
उदाहरण: Anonymous Functions as Arguments
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"sort"
)
// main is where the program starts running
func main() {
// Short declaration: create nums and infer the type
nums := []int{5, 2, 8, 1}
sort.Slice(nums, func(i, j int) bool {
// Send nums[i] < nums[j] back to the caller
return nums[i] < nums[j]
})
// Print the values followed by a newline
fmt.Println(nums)
}
Login to try C/C++/Java/PHP code in the editor
- किसी variable को assign किए anonymous function को func keyword के अंदर एक अनावश्यक नाम देना, जिसकी Go अनुमति नहीं देता (func literals का कोई नाम नहीं होता)।
- किसी anonymous function literal को असल में तुरंत invoke करने (एक IIFE) के लिए इसके बाद '()' जोड़ना भूल जाना, इसे सिर्फ defined छोड़ते हुए लेकिन कभी call न करते हुए।
- गहराई से nested anonymous functions का ज़्यादा उपयोग करना जहां एक छोटा named function कहीं ज़्यादा readable होता।
- एक anonymous function बिना नाम वाला एक function literal है, जो इसके उपयोग होने की जगह inline defined है।
- Anonymous functions को एक variable को assign किया जा सकता है और इसके ज़रिए call किया जा सकता है।
- उन्हें definition के बाद literal में () जोड़कर तुरंत invoke भी किया जा सकता है।
- वे आमतौर पर छोटे callback arguments या goroutines के लिए उपयोग होते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: