go - 不改变接收器的接口(interface)

标签 go

我想定义一个接口(interface),它有一个方法返回一个类型为接口(interface)本身的值。

我试过这样定义接口(interface):

type Event interface {
}

type Entity interface {
    ApplyEvent(command Event) (Entity, error)
}

我想通过以下方式使结构实现实体接口(interface):

type ShoppingList struct {
}

func (list ShoppingList) ApplyEvent(event Event) (ShoppingList, error) {
    // code that changes "list" goes here.
    return list, nil
}

如果我这样做,然后尝试将 ShoppingList 传递给需要实体的函数,我会收到以下错误:

func main() {
    test(ShoppingList{})
}

func test(e Entity) {
}

Cannot use 'ShoppingList{}' (type ShoppingList) as type Entity. 
Type does not implement 'Entity' 
need method: ApplyEvent(command Event) (Entity, error) 
have method: ApplyEvent(event Event) (ShoppingList, error)
   

我知道我可以这样定义接口(interface)和接收器:

type Event interface {
}

type Entity interface {
    ApplyEvent(command Event) error
}

type ShoppingList struct {
}

func (list *ShoppingList) ApplyEvent(event Event) error {
    // code that changes "list" goes here.
    return nil
}

但我更愿意尽可能使用纯函数和不可变数据结构来编写代码。

我想返回更改后的值而不是改变接收者。

在 Go 中有什么方法可以做到这一点?

最佳答案

看来您可能已经知道这一点。但以防万一你还没有想到,你也可以这样写:

type Event interface {
}

type Entity interface {
    ApplyEvent(command Event) (Entity, error)
}

type ShoppingList struct {
}

func (list ShoppingList) ApplyEvent(event Event) (Entity, error) {
    //...
    return list
}

在这里,我正在执行相同的 return,但我将其“作为”Entity 接口(interface)而不是 ShoppingList 返回。如果稍后 Entity 是一个购物 list 是相关的,如果我想查看 Entity 是否是一个 ShoppingList,我可以尝试类型断言> 稍后在代码中。

但是,为 ShoppingList 提供一个接口(interface)方法以使其作为一个实体<来完成它的事情,这将更符合 interface 概念/em> 而不是消费者枚举所有可能的实体。毕竟,为什么应用于“ShoppingList”的“Event”一定会产生另一个“ShoppingLIst”?它不能生成例如 InstacartInvoice 吗?当然,在这一点上我远远超出了你的问题范围。但是任何时候接口(interface)的具体值的类型与消费者相关时,请非常努力地使其与该接口(interface)的方法相关。就像您对 ApplyEvent 所做的那样。

关于go - 不改变接收器的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64405031/

相关文章:

CORS 策略不允许和阻止 Angular 前端 POST 到 Golang 后端方法

php - 迭代从 PHP 序列化格式解码的 map

xml - 在 golang 中解码 XML

xml - Golang XML 解析问题

go - 使用提供的公钥验证 ECDSA 签名

json - golang GET请求运行时json类型错误

go - 无法在 http 请求中设置 POST 正文

gorilla /websocket 设置读取限制?

docker - 无法安装 golint 包 : wrong import path

json - golang : Unmarshal: json: cannot unmarshal array into Go value of type main. MonitorServerInfo