Changes to interfaces in Go1.14

Dylan Meeus
2 min readFeb 8, 2020

--

Go Gopher by Renee French

Keeping up with their six-monthly release schedule, Go 1.14 is just around the corner. There’s quite a few changes such as a faster hash function, and the removal of SSLv3 (which is a breaking change).

But one that I’ll focus on in this post is a change to the core language, namely how interfaces work. Before Go 1.14, you could not have overlapping interfaces. If you’d compose an interface, and your ‘base interfaces’ have a similar function, you’d get the error:

duplicate method methodName()

For example, if you have two simple interfaces:

type A interface{
DoX()
DoY()
}

type B interface{
DoX()
}

You can create a struct that satisfies both interfaces at the same time, without an issue:

type S struct {}
func (s S) DoX() {}
func (s S) DoY() {}

Which means S implements both A and B.

Yet, if we’d create an interface that is a composite of A and B, we’ll get compile-time failures.

type AB interface{
A
B
}

As of Go 1.14, creating such composite interfaces will be possible (as long as the functions hold the same signature). So this will still fail:

type A interface{
DoX() string
}
type B interface{
DoX()
}
type AB interface{
A
B
}

At this point, the function signature of DoX() is different for interface A and B which is an actual problem.

Personally I’m quite happy with this change, I consider it a bug that this did not work in earlier versions of Go as there would never be any ambiguity when the function signature is the same.

There’s an interesting discussion around this topic that started about seven(!) years ago on github: https://github.com/golang/go/issues/6977

If you want to try out this code (which is on Github) and you don’t want to wait for 1.14 to actually be released, you can run it with the beta version of 1.14. One of the easiest ways to install this in my opinion is with go get.

go get golang.org/dl/go1.14beta1
go1.14beta1 download

After which you can use the go1.14beta1 command as a replacement for the usual go command. Thus to run the code you use go1.14beta1 run main.go 😉

If you liked this post and 💙 Go as well, consider:

  • Following me here, on Medium
  • Or twitter Twitter
  • Or check out workwithgo.com to find cool Go jobs around the world

--

--