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

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

Golang equivalent to Python's NotImplementedException

问题

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

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

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

这是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:

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
}

答案1

得分: 6

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

<details>
<summary>英文:</summary>

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


</details>



# 答案2
**得分**: 3

以下是从我在Go中实现gRPC的示例生成的代码:

```go
import (
	status "google.golang.org/grpc/status"
)

// . . .

// UnimplementedInstanceControlServer可以嵌入以具有向前兼容的实现。
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以满足方法合同。

英文:

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

import (
	status &quot;google.golang.org/grpc/status&quot;
)

// . . .

// 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, &quot;method HealthCheck not implemented&quot;)
}

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

答案3

得分: 2

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

type MyInterface interface {
    Method1() bool
    Method2() (bool, error)
}

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

英文:

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

type MyInterface interface {
    Method1() bool
    Method2() (bool, error)
}

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

答案4

得分: 2

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

import (
    "errors"
    "fmt"
)

func (t *Thing) Method2() (bool, error) {
    // 我还不想实现这个方法
    return nil, errors.New("未实现")
    // 也可以使用 fmt.Errorf("未实现")
}

func (t *Thing) Method3() (bool, error) {    
    return nil, fmt.Errorf("未实现")
}

以上是代码的翻译部分。

英文:

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

import (
	&quot;errors&quot;
    &quot;fmt&quot;
)

func (t *Thing) Method2() (bool, error) {
    // I don&#39;t want to implement this yet
   return nil, errors.New(&quot;Not implemented&quot;)
   // Also return fmt.Errorf(&quot;Not implemented&quot;)
}

func (t *Thing) Method3() (bool, error) {    
   return nil, fmt.Errorf(&quot;Not implemented&quot;)
}

答案5

得分: 1

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

var _ MyInterface = &Thing{}

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

英文:

a empty var will do this

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:

确定