在Go语言中,aws-sdk-v2中的接口重新声明是否正确?

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

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)

这段代码是否正确?接口中是否可以重新声明函数?我应该如何解决这个问题?

英文:

In aws-sdk-v2 library for Go, we have the following interfaces definitions:

type Retryer interface {
    GetInitialToken() (releaseToken func(error) error)
}


type RetryerV2 interface {
	Retryer
	
	GetInitialToken() (releaseToken func(error) error)
}

(the code is here: https://github.com/aws/aws-sdk-go-v2/blob/main/aws/retryer.go)

This causes compilation error:

aws/retryer.go:81: GetInitialToken redeclared (compile)

Is this code correct or not? Is it possible to redeclare function in of interfaces?
How am I supposed to solve this problem?

答案1

得分: 1

你可能正在使用一个旧版本的Go。自Go 1.14版本以来,允许重叠的方法集,并且该代码可以在Go Playground上编译。

引用自Go 1.14版本发布日志:

根据重叠接口提案,Go 1.14现在允许嵌入具有重叠方法集的接口:来自嵌入接口的方法可以与(嵌入)接口中已经存在的方法具有相同的名称和相同的签名。这解决了通常(但不仅限于)在菱形嵌入图中出现的问题。接口中明确声明的方法必须保持唯一性,与以前一样。

如果你在发布的代码中遇到编译时错误,那说明你正在使用早于1.14版本的Go。请紧急更新!请注意,只支持最后2个主要版本(目前为1.17和1.16)。使用1.13这样的版本是一个重大风险!

英文:

Likely you are using an old version of Go. Overlapping method sets are allowed since Go 1.14, and the code compiles on the Go Playground.

Quoting from Go 1.14 release log:

> 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.

If you get a compile-time error for the code you posted, that suggests you're using a Go prior to 1.14. Urgently update! Please note that only the last 2 major versions are supported (currently 1.17 and 1.16). You using a version like 1.13 is a major risk!

huangapple
  • 本文由 发表于 2022年2月28日 16:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/71292588.html
匿名

发表评论

匿名网友

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

确定