Closures क्या हैं
In this page:
func outer() func(param type) returnType {
captured := value
return func(param type) returnType {
// uses captured
}
}
What Makes a Closure
एक closure बस एक function literal है जो अपने बाहर declared किसी variable को refer करता है -- Go अपने आप उस variable को उस function के लिए जीवित और accessible रखता है जब तक closure खुद मौजूद रहे।
उदाहरण: What Makes a Closure
// 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 message and infer the type
message := "hello"
// Short declaration: create greet and infer the type
greet := func() {
// Print the values followed by a newline
fmt.Println(message)
}
greet()
}
Login to try C/C++/Java/PHP code in the editor
Closures Share, Not Copy, Captured Variables
चूंकि एक closure अपनी value के snapshot के बजाय variable को खुद capture करता है, closure के अंदर किए बदलाव इसके बाहर दिखाई देते हैं, और बाहर किए बदलाव अगली बार closure चलने पर दिखाई देते हैं।
उदाहरण: Closures Share, Not Copy, Captured Variables
// 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 count and infer the type
count := 0
// Short declaration: create increment and infer the type
increment := func() {
count++
}
increment()
increment()
// Print the values followed by a newline
fmt.Println("count:", count)
}
Login to try C/C++/Java/PHP code in the editor
Building a Counter Factory
Closures का एक classic उपयोग एक ऐसा function है जो दूसरा function return करता है, जो calls के बीच अपनी private state जीवित रखता है -- जैसे एक counter generator जो हर call पर एक independent, incrementing counter सौंपता है।
उदाहरण: Building a Counter Factory
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function makeCounter
func makeCounter() func() int {
// Short declaration: create count and infer the type
count := 0
// Send func() int { back to the caller
return func() int {
count++
return count
}
}
// main is where the program starts running
func main() {
// Short declaration: create counter and infer the type
counter := makeCounter()
// Print the values followed by a newline
fmt.Println(counter())
fmt.Println(counter())
fmt.Println(counter())
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि हर closure को किसी captured variable की अपनी independent copy मिलती है, जबकि closures असल में उसी variable का एक reference share करते हैं।
- Go-1.22 से पहले किसी loop में closures बनाना और यह देखकर हैरान होना कि वे सभी एक जैसी आखिरी loop variable value capture करते हैं।
- किसी भी synchronization के बिना कई goroutines से shared state को mutate करने के लिए एक closure उपयोग करना, एक data race का कारण बनते हुए।
- एक closure एक function literal है जो अपनी body के बाहर से variables को reference करता है।
- Captured variables reference से share होते हैं, copy नहीं होते -- closure के अंदर बदलाव बाहर दिखाई देते हैं, और उल्टा भी।
- Closures आमतौर पर function factories बनाने के लिए उपयोग होते हैं, जैसे counters या accumulators।
- Go 1.22 से, हर loop iteration को एक नया variable मिलता है, loops के अंदर closures को सहज रूप से व्यवहार करने देते हुए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: