Sentinel Errors क्या हैं
In this page:
var ErrName = errors.New("message")
if err == ErrName {
// handle
}
Defining a Sentinel Error
एक sentinel error बस एक package-level variable है जो errors.New से एक बार बनाया जाता है, message text के बजाय identity से compare होने के लिए बनाया गया -- यह एक specific failure condition के लिए एक जाना-पहचाना signal की तरह काम करता है।
उदाहरण: Defining a Sentinel Error
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"errors"
"fmt"
)
var ErrNotFound = errors.New("resource not found")
// Define the function find
func find(id int) error {
// Check whether id != 1
if id != 1 {
// Send ErrNotFound back to the caller
return ErrNotFound
}
// Send nil back to the caller
return nil
}
// main is where the program starts running
func main() {
// Short declaration: create err and infer the type
err := find(2)
fmt.Println(err)
}
Login to try C/C++/Java/PHP code in the editor
Checking Against a Sentinel
Calling code errors.Is से किसी sentinel error को जांचता है, returned error को known sentinel value से compare करते हुए -- यह सही से match करता है भले ही error रास्ते में wrapped हुई हो।
उदाहरण: Checking Against a Sentinel
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"errors"
"fmt"
)
var ErrNotFound = errors.New("resource not found")
// Define the function find
func find(id int) error {
// Check whether id != 1
if id != 1 {
// Send ErrNotFound back to the caller
return ErrNotFound
}
// Send nil back to the caller
return nil
}
// main is where the program starts running
func main() {
// Short declaration: create err and infer the type
err := find(5)
if errors.Is(err, ErrNotFound) {
fmt.Println("handle missing resource specifically")
}
}
Login to try C/C++/Java/PHP code in the editor
Sentinel Errors in the Standard Library
Go की standard library इसी exact pattern पर निर्भर है -- io.EOF किसी stream के अंत का संकेत देता है, और किसी io.Reader से पढ़ने वाला code done को असली failure से अलग करने के लिए errors.Is(err, io.EOF) से इसे जांचता है।
उदाहरण: Sentinel Errors in the Standard Library
package main
import (
"errors"
"fmt"
"io"
"strings"
)
func main() {
r := strings.NewReader("hi")
buf := make([]byte, 10)
_, err := r.Read(buf)
_, err = r.Read(buf) // second read hits the end
if errors.Is(err, io.EOF) {
fmt.Println("reached end of stream")
}
}
Login to try C/C++/Java/PHP code in the editor
- errors.Is के बजाय == से किसी sentinel error को संभावित-wrapped error से compare करना।
- हर बार एक shared package-level sentinel को reuse करने के बजाय हर बार एक नया errors.New("not found") बनाना, equality checks तोड़ते हुए।
- sentinel errors को conventional Err prefix से नाम न देना, उन्हें पहचानना और document करना मुश्किल बनाते हुए।
- एक sentinel error एक package-level error value है, conventionally ErrSomething नाम की, errors.New से एक बार बनाई गई।
- Callers इसे errors.Is(err, ErrSomething) से जांचते हैं, जो wrapped errors के ज़रिए भी काम करता है।
- Sentinel errors specific known failure conditions का संकेत देने के लिए एक छोटे, stable public API की तरह काम करते हैं।
- Standard library इस pattern का व्यापक उपयोग करती है, जैसे io.EOF और sql.ErrNoRows।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: