Go doesn't support try-catch, Instead Go coding style for error handling is to return an error along side the potential valid value. If an error accord the error will be set with implementation of the error
interface, otherwise it will be set to nil
.
See the flowing signature:
func Open(name string) (file *File, err error)
I would like to know what is the name of this "error handling" pattern.
CodePudding user response:
I think that what you're looking for is named status return in the literature. Golang just implements it by the consideration an error (or faulty status) is defined as the unwanted and unusual conditions encountered in the program.
Meanwhile already in 2003 (six years before Golang was launched) Ned Batchelder wrote about the benefits of Exceptions vs Status Returns (in this case focused on C ) by pointing out some benefits as cleaner code, Joel Spolsky also showed his point of view, but in this case in favour of the status' return opposed to Exceptions.
Way after the creation of Go, in 2014, Martin Fowler also wrote about returning errors in form of Notifications instead of throwing Exceptions.