2 ms·
Actually, your example would continue running the happy path as if the error didn't happen, returning erroneous results, but not panicking: func main() {
by bheadmaster 4y ago
Actually, your example would continue running the happy path as if the error didn't happen, returning erroneous results, but not panicking:
func main() {
x, _ := getNonZero()
fmt.Println("Nonzero number:", x)
}
func getNonZero() (int, error) {
return 0, errors.New("oops")
}
The "_" is supposed to be used only when you're absolutely sure that error cannot happen (e.g. the function you're calling is partial, but you know that the input is in the defined set).