defer Statement क्या है
In this page:
defer functionCall(args)
Basic defer
एक defer statement एक function call को surrounding function के return होने से ठीक पहले execute होने के लिए schedule करता है, चाहे यह कैसे भी return हो (panic के ज़रिए भी)।
यह guaranteed cleanup code के लिए Go का मुख्य tool है, दूसरी languages में finally blocks जैसा।
उदाहरण: Basic defer
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function greet
func greet() {
// defer runs this call when the surrounding function returns
defer fmt.Println("goodbye")
// Print the values followed by a newline
fmt.Println("hello")
}
// main is where the program starts running
func main() {
greet()
}
Login to try C/C++/Java/PHP code in the editor
LIFO Order of Multiple Defers
जब किसी function में कई defer statements हों, वे last-in-first-out order में execute होते हैं: सबसे हाल ही में deferred call पहले चलती है। यह उस तरीके को दर्शाता है जिससे आप nested resources के एक stack को unwind करना चाहेंगे।
उदाहरण: LIFO Order of Multiple Defers
// 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() {
// defer runs this call when the surrounding function returns
defer fmt.Println("first deferred (runs last)")
// defer runs this call when the surrounding function returns
defer fmt.Println("second deferred (runs middle)")
defer fmt.Println("third deferred (runs first)")
// Print the values followed by a newline
fmt.Println("main body")
}
Login to try C/C++/Java/PHP code in the editor
Arguments Are Evaluated Immediately
defer की एक सूक्ष्मता यह है कि deferred function के arguments उसी क्षण evaluate होते हैं जब defer statement चलता है, बाद में जब deferred call असल में fire होती है तब नहीं -- सिर्फ call खुद postpone होती है।
उदाहरण: Arguments Are Evaluated Immediately
package main
import "fmt"
func main() {
x := 1
defer fmt.Println("deferred x was:", x) // captures x=1 now
x = 99
fmt.Println("current x:", x)
}
Login to try C/C++/Java/PHP code in the editor
defer for Cleanup
defer का सबसे आम असली-दुनिया उपयोग यह गारंटी देना है कि किसी function की शुरुआत में acquire किया गया एक resource -- एक file, एक lock, एक network connection -- चाहे function कोई भी return path ले, release हो जाए।
उदाहरण: defer for Cleanup
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function process
func process() {
// Print the values followed by a newline
fmt.Println("acquiring resource")
// defer runs this call when the surrounding function returns
defer fmt.Println("releasing resource")
// Print the values followed by a newline
fmt.Println("using resource")
}
// main is where the program starts running
func main() {
process()
}
Login to try C/C++/Java/PHP code in the editor
- किसी tight loop के अंदर defer करना (जैसे एक ही function में हज़ारों files के लिए file.Close() defer करना) जो ढेर हो जाता है और पूरे function के return होने तक cleanup टाल देता है।
- यह मान लेना कि deferred function arguments deferred call चलने पर evaluate होते हैं, जबकि वे असल में defer statement पर तुरंत evaluate होते हैं।
- यह उम्मीद करना कि कई defers उस order में चलेंगे जिसमें वे लिखे गए थे -- वे असल में last-in-first-out (LIFO) order में चलते हैं।
- defer enclosing function के return होने से ठीक पहले चलने के लिए एक function call schedule करता है।
- कई defers होने पर deferred calls last-in-first-out order में चलते हैं।
- किसी deferred call के arguments तुरंत evaluate होते हैं, deferred call असल में चलने पर नहीं।
- defer किसी file को बंद करने या mutex unlock करने जैसी guaranteed cleanup का idiomatic तरीका है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: