4 ms·
In Go, a type doesn't have to declare that it implements an interface; it automatically satisfies an interface if it implements the appropriate methods. Most in
by usea 13y ago
In Go, a type doesn't have to declare that it implements an interface; it automatically satisfies an interface if it implements the appropriate methods. Most interfaces in Go only contain one method.
If a type has a Close() method, then it is a Closer. The code that tries to cast it to a Closer will succeed. So to answer your question, it doesn't matter what the semantics of your Close() method are, it will succeed solely based on the fact that your type has a method with that name.
- melvinmt 13y ago> your type has a method with that name. I would say with the same function signature, not just the name.
- usea 13y agoGo doesn't support overloading. Method dispatch is done by name only. http://golang.org/doc/faq#overloading http://golang.org/doc/faq#overloading
- statusgraph 13y agoIt doesn't work :) http://play.golang.org/p/PVS5sarmHb http://play.golang.org/p/PVS5sarmHb
- dragonwriter 13y agoBut interface satisfaction is by signature, not name, and that is what is being discussed, not method dispatch.
- usea 13y agoSorry, I didn't mean to conflate the two. Thanks for the clarification.