aws-sdk-v2 中 Go 中的接口重新声明:是否正确?
Interface redeclaration in Go in aws-sdk-v2: is it correct?
在 Go 的 aws-sdk-v2 库中,我们有以下接口定义:
type Retryer interface {
GetInitialToken() (releaseToken func(error) error)
}
type RetryerV2 interface {
Retryer
GetInitialToken() (releaseToken func(error) error)
}
(代码在这里:https://github.com/aws/aws-sdk-go-v2/blob/main/aws/retryer.go)
这会导致编译错误:
aws/retryer.go:81: GetInitialToken redeclared (compile)
这段代码是否正确?是否可以在接口中重新声明函数?
我该如何解决这个问题?
您可能使用的是旧版本的 Go。自 Go 1.14 起允许重叠方法集,并且代码在 Go Playground.
上编译
Per the overlapping interfaces proposal, Go 1.14 now permits embedding of interfaces with overlapping method sets: methods from an embedded interface may have the same names and identical signatures as methods already present in the (embedding) interface. This solves problems that typically (but not exclusively) occur with diamond-shaped embedding graphs. Explicitly declared methods in an interface must remain unique, as before.
如果您发布的代码出现 compile-time 错误,则表明您使用的是 1.14 之前的 Go。紧急更新!请注意,仅支持最后两个主要版本(目前为 1.17 和 1.16)。你用1.13这样的版本风险很大!
在 Go 的 aws-sdk-v2 库中,我们有以下接口定义:
type Retryer interface {
GetInitialToken() (releaseToken func(error) error)
}
type RetryerV2 interface {
Retryer
GetInitialToken() (releaseToken func(error) error)
}
(代码在这里:https://github.com/aws/aws-sdk-go-v2/blob/main/aws/retryer.go)
这会导致编译错误:
aws/retryer.go:81: GetInitialToken redeclared (compile)
这段代码是否正确?是否可以在接口中重新声明函数? 我该如何解决这个问题?
您可能使用的是旧版本的 Go。自 Go 1.14 起允许重叠方法集,并且代码在 Go Playground.
上编译Per the overlapping interfaces proposal, Go 1.14 now permits embedding of interfaces with overlapping method sets: methods from an embedded interface may have the same names and identical signatures as methods already present in the (embedding) interface. This solves problems that typically (but not exclusively) occur with diamond-shaped embedding graphs. Explicitly declared methods in an interface must remain unique, as before.
如果您发布的代码出现 compile-time 错误,则表明您使用的是 1.14 之前的 Go。紧急更新!请注意,仅支持最后两个主要版本(目前为 1.17 和 1.16)。你用1.13这样的版本风险很大!