Ale*_*lex 10 python oop error-handling idioms go
NotImplementedException当您定义一个带有您不想实现的方法的接口时,Golang 中是否有相当于在 Python 中引发 a 的方法?这是惯用的 Golang 吗?
例如:
type MyInterface interface {
Method1() bool
Method2() bool
}
// Implement this interface
type Thing struct {}
func (t *Thing) Method1() bool {
return true
}
func (t *Thing) Method2() bool {
// I don't want to implement this yet
}
Run Code Online (Sandbox Code Playgroud)
通常在 golang 中如果你想实现错误处理你会返回一个错误
type MyInterface interface {
Method1() bool
Method2() (bool, error)
}
Run Code Online (Sandbox Code Playgroud)
然后就可以返回错误了。您还可以记录或恐慌,如 @coredump 在评论中所说。
| 归档时间: |
|
| 查看次数: |
8561 次 |
| 最近记录: |