简体   繁体   English

Go错误返回模式的名称是什么?

[英]What is the name of Go error return pattern?

Go doesn't support try-catch, Instead Go coding style for error handling is to return an error along side the potential valid value. Go 不支持 try-catch,相反,错误处理的 Go 编码风格是在返回潜在有效值的同时返回一个错误。 If an error accord the error will be set with implementation of the error interface, otherwise it will be set to nil .如果错误与error接口的实现一致,错误将被设置,否则它将被设置为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.我想知道这个“错误处理”模式的名称是什么。

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. Golang 只是通过考虑将错误(或故障状态)定义为程序中遇到的不需要和不寻常的情况来实现它。

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 .与此同时,早在2003 年Golang 推出前六年),Ned Batchelder就 Exceptions 与 Status Returns 的好处(在本例中专注于 C++)指出了一些更清晰的代码的好处,Joel Spolsky 也表达了他的观点,但是在这种情况下,支持 status' return 反对 Exceptions

Way after the creation of Go, in 2014, Martin Fowler also wrote about returning errors in form of Notifications instead of throwing Exceptions.在创建 Go 之后,在 2014 年, Martin Fowler 还写了关于以通知的形式返回错误而不是抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM