Interface redeclaration in Go in aws-sdk-v2: is it correct?

Issue

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?

Solution

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!

Answered By – icza

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.