在Golang中有没有等同于在Python中引发NotImplementedException的方法,当您定义一个带有您还不想实现的方法的接口时?这是习语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
}发布于 2016-12-15 00:16:49
通常在golang中,如果您想实现错误处理,则返回一个错误
type MyInterface interface {
Method1() bool
Method2() (bool, error)
}然后你可以返回一个错误。你也可以记录日志,或者像@coredump在评论中所说的那样恐慌。
发布于 2020-01-12 02:04:34
下面是我在Go中实现gRPC时生成的一个示例:
import (
status "google.golang.org/grpc/status"
)
// . . .
// UnimplementedInstanceControlServer can be embedded to have forward compatible implementations.
type UnimplementedInstanceControlServer struct {
}
func (*UnimplementedInstanceControlServer) HealthCheck(ctx context.Context, req *empty.Empty) (*HealthCheckResult, error) {
return nil, status.Errorf(codes.Unimplemented, "method HealthCheck not implemented")
}或者,您可以在方法中记录错误,然后返回nil以满足方法约定。
发布于 2021-06-25 01:22:24
func someFunc() {
panic("someFunc not implemented")
}https://stackoverflow.com/questions/41147191
复制相似问题