On 22/09/22, burak serdar (bser...@computer.org) wrote: > On Thu, Sep 22, 2022 at 4:27 PM Rory Campbell-Lange <r...@campbell-lange.net> > wrote: > > > ...I'm interested to learn how people negotiate interface > > interchangeability in their programmes as my query above showed a basic > > misunderstanding of how that operates and how to approach the topic in > > general. > > ... > > ...Jordan Orelli uses the example of "...the Animal type will be an > > interface, and we’ll define an Animal as being anything that can speak". He > > writes: > > I think one of the problems in this particular example is that it is for > some reason easy for people to accept Animal type to be something that can > speak. I can see that it is simply an example to show the mechanics of > interfaces, but it also sets the expectation that the interface name means > more than what it actually is: in Go, an interface is simply a method set. > This is a difficult concept for people coming from other languages. So you > can have: > > if speaker, yes:=someInterfaceValue.(interface{Speak() string}); yes { > spearer.Speak() > } > > without having a named interface at all. > > That is one of the properties of duck-typing I enjoy, especially after > working with Java for many years. In Java, an interface is a contract. In > Go, an interface is just a method set, which can be used as a contract.
Thank you for thoughtful comment about interface names being simply labels on method sets. In this case the compiler did not enforce the contract. (As I noted in my original email, converting an fs.File to an io.ReadSeeker for an underlying zip file failed, but only at runtime). If interfaces provide contracts, though, it would be great if type "interface satisfaction"[1] could be more thoroughly checked at compile time. Regards, Rory [1] The description "Interface Satisfaction" is used in section 7.3 of "The Go Programming Language" -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/Yy2f9SwcoFVPDCbX%40campbell-lange.net.