首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Golang相当于Python的NotImplementedException

Golang相当于Python的NotImplementedException
EN

Stack Overflow用户
提问于 2016-12-15 00:11:27
回答 5查看 3.3K关注 0票数 3

在Golang中有没有等同于在Python中引发NotImplementedException的方法,当您定义一个带有您还不想实现的方法的接口时?这是习语Golang吗?

例如:

代码语言:javascript
运行
复制
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
}
EN

回答 5

Stack Overflow用户

发布于 2016-12-15 00:16:49

通常在golang中,如果您想实现错误处理,则返回一个错误

代码语言:javascript
运行
复制
type MyInterface interface {
    Method1() bool
    Method2() (bool, error)
}

然后你可以返回一个错误。你也可以记录日志,或者像@coredump在评论中所说的那样恐慌。

票数 2
EN

Stack Overflow用户

发布于 2020-01-12 02:04:34

下面是我在Go中实现gRPC时生成的一个示例:

代码语言:javascript
运行
复制
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以满足方法约定。

票数 2
EN

Stack Overflow用户

发布于 2021-06-25 01:22:24

代码语言:javascript
运行
复制
func someFunc() {
   panic("someFunc not implemented")
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41147191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档