આ ત્રણ keywords deferred execution અને exceptional situations સાથે કામ કરે છે. defer cleanup શેડ્યુલ કરે છે, panic runtime crash ટ્રિગર કરે છે (સાચમાં exceptional cases માટે), અને recover panic પકડી શકે છે તેથી program crash ન થાય.
આ ત્રણ keywords deferred execution અને exceptional situations સાથે કામ કરે છે. defer cleanup શેડ્યુલ કરે છે, panic runtime crash ટ્રિગર કરે છે (સાચમાં exceptional cases માટે), અને recover panic પકડી શકે છે તેથી program crash ન થાય.
func readFile() error {
f, err := os.Open("file.txt")
if err != nil { return err }
defer f.Close() // GUARANTEED to run when readFile returns (any path)
// ... use f, with multiple return points ...
return nil // f.Close() runs here automatically
}
defer એક function call ને execute કરવા માટે શેડ્યુલ કરે છે જ્યારે surrounding function return થાય — તે કેવી રીતે return થાય તે બાબત નથી (normal return, error, અથવા panic). તે cleanup સુનિશ્ચિત કરવાનો idiomatic way છે: files બંધ કરવી, mutexes unlock કરવી, connections બંધ કરવી — acquisition ની બાજુમાં મૂકવું તેથી તમે તે ભૂલી શકો નહીં.
defer fmt.Println("1")
defer fmt.Println("2")
defer fmt.Println("3")
// prints: 3, 2, 1 — last deferred, first executed (stack order)
func mustPositive(n int) {
if n < 0 {
panic("negative not allowed") // stops normal execution, unwinds the stack
}
}
// panic runs deferred functions as it unwinds, then crashes the program (with a stack trace)
panic normal flow બંધ કરે છે અને call stack unwind કરે છે (deferred functions સાથે), આખરે program crash થાય છે. તે programmer errors / unrecoverable conditions માટે છે (out-of-bounds, nil dereference, impossible states) — ordinary errors માટે નહીં (તે returned error values વાપરે છે).
func safeProcess() {
defer func() {
if r := recover(); r != nil { // recover() returns the panic value
fmt.Println("recovered from:", r) // handle it; program continues
}
}()
panic("something broke") // this panic is caught by the recover above
}
// safeProcess returns normally instead of crashing
recover panicking goroutine પર નિયંત્રણ પુનઃપ્રાપ્ત કરે છે — પણ માત્ર deferred function માં કામ કરે છે. તે rarely વાપર્યું છે, જેમ કે એક request handler ને સમગ્ર server ને crash કરતા અટકાવવા માટે.
Normal/expected failures (file missing, bad input, validation) → return an error value
panic/recover → reserve for TRULY exceptional cases (bugs, unrecoverable states)
and boundaries (e.g. a server recovering so one bad request doesn't kill the process)
defer એક essential, idiomatic Go feature છે guaranteed cleanup માટે — resource release ને acquisition ની બાજુમાં મૂકવું અને તે every return path પર run કરવું સુનિશ્ચિત કરવું (panics સહિત), જે leaked files, locks, અને connections ને manual cleanup કરતાં વધુ reliably રોકે છે.
તેના LIFO ordering ને સમજવું પણ મહત્વપૂર્ણ છે. panic/recover exception-like mechanism આપે છે, પણ critical principle એ છે કે Go તેમને genuinely exceptional, unrecoverable situations માટે સંરક્ષિત કરે છે — normal errors હંમેશા values તરીકે return કર્યા અને checked જોઈએ.
General exception handling તરીકે panic/recover ને misuse કરવું un-idiomatic છે અને exception-based languages પાસેથી આવતા developers માટે common mistake છે.
defer (હંમેશા, cleanup માટે), error returns (normal failures), અને panic/recover (rare, exceptional, અથવા boundaries પર) વાપર્યો ક્યારે કરવો તે જાણવું fundamental છે correct, idiomatic Go લખવા માટે અને તે frequent interview topic છે.