Golang中与Python的NotImplementedException相对应的是什么?

huangapple go评论121阅读模式
英文:

Golang equivalent to Python's NotImplementedException

问题

在Golang中,没有与Python中的NotImplementedException相对应的等效方式来定义一个接口并标记尚未实现的方法。在Golang中,接口的方法必须在实现接口的类型中完全实现。

如果你不想实现接口的某些方法,可以将它们留空或者返回一个默认值。在你的示例中,你可以将Method2留空或者返回一个默认的布尔值,如下所示:

  1. func (t *Thing) Method2() bool {
  2. return false // 或者返回其他默认值
  3. }

这是Golang中的一种惯用方式,表示该方法暂时没有实现。

英文:

Is there an equivalent in Golang to raising a NotImplementedException in Python when you define an interface with methods that you don't want to implement yet? Is this idiomatic Golang?

For example:

  1. type MyInterface interface {
  2. Method1() bool
  3. Method2() bool
  4. }
  5. // Implement this interface
  6. type Thing struct {}
  7. func (t *Thing) Method1() bool {
  8. return true
  9. }
  10. func (t *Thing) Method2() bool {
  11. // I don't want to implement this yet
  12. }

答案1

得分: 6

  1. func someFunc() {
  2. panic("someFunc not implemented")
  3. }
  1. <details>
  2. <summary>英文:</summary>

func someFunc() {
panic("someFunc not implemented")
}

  1. </details>
  2. # 答案2
  3. **得分**: 3
  4. 以下是从我在Go中实现gRPC的示例生成的代码:
  5. ```go
  6. import (
  7. status "google.golang.org/grpc/status"
  8. )
  9. // . . .
  10. // UnimplementedInstanceControlServer可以嵌入以具有向前兼容的实现。
  11. type UnimplementedInstanceControlServer struct {
  12. }
  13. func (*UnimplementedInstanceControlServer) HealthCheck(ctx context.Context, req *empty.Empty) (*HealthCheckResult, error) {
  14. return nil, status.Errorf(codes.Unimplemented, "method HealthCheck not implemented")
  15. }

或者,您可以在方法内部记录错误,然后返回nil以满足方法合同。

英文:

Here's an example that was generated from my implementation of gRPC in Go:

  1. import (
  2. status &quot;google.golang.org/grpc/status&quot;
  3. )
  4. // . . .
  5. // UnimplementedInstanceControlServer can be embedded to have forward compatible implementations.
  6. type UnimplementedInstanceControlServer struct {
  7. }
  8. func (*UnimplementedInstanceControlServer) HealthCheck(ctx context.Context, req *empty.Empty) (*HealthCheckResult, error) {
  9. return nil, status.Errorf(codes.Unimplemented, &quot;method HealthCheck not implemented&quot;)
  10. }

Alternatively, you could log an error inside the method and then return a nil to satisfy the method contract.

答案3

得分: 2

通常在Go语言中,如果你想实现错误处理,你可以返回一个错误。

  1. type MyInterface interface {
  2. Method1() bool
  3. Method2() (bool, error)
  4. }

然后你可以返回一个错误。你也可以像评论中的@coredump所说的那样进行日志记录或者抛出异常。

英文:

Usually in golang if you want to implement error handling you return an error

  1. type MyInterface interface {
  2. Method1() bool
  3. Method2() (bool, error)
  4. }

Then you can return an error.
You can also log, or panic as @coredump said in the comments.

答案4

得分: 2

这是一个常见的Go语言模式,即在发生错误时返回结果或错误信息。

  1. import (
  2. "errors"
  3. "fmt"
  4. )
  5. func (t *Thing) Method2() (bool, error) {
  6. // 我还不想实现这个方法
  7. return nil, errors.New("未实现")
  8. // 也可以使用 fmt.Errorf("未实现")
  9. }
  10. func (t *Thing) Method3() (bool, error) {
  11. return nil, fmt.Errorf("未实现")
  12. }

以上是代码的翻译部分。

英文:

It's a common pattern in go, that you return your result or error in case of failure.

  1. import (
  2. &quot;errors&quot;
  3. &quot;fmt&quot;
  4. )
  5. func (t *Thing) Method2() (bool, error) {
  6. // I don&#39;t want to implement this yet
  7. return nil, errors.New(&quot;Not implemented&quot;)
  8. // Also return fmt.Errorf(&quot;Not implemented&quot;)
  9. }
  10. func (t *Thing) Method3() (bool, error) {
  11. return nil, fmt.Errorf(&quot;Not implemented&quot;)
  12. }

答案5

得分: 1

一个空的变量将会执行以下操作:

  1. var _ MyInterface = &Thing{}

如果Thing没有实现接口MyInterface,编译将会失败。

英文:

a empty var will do this

  1. var _ MyInterface = &amp;Thing{}

if Thing doesn't implement the interface MyInterface, compile will fail

huangapple
  • 本文由 发表于 2016年12月15日 00:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/41147191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定