4 ms·
The whimsical nature of interfaces is definitely different coming from other languages. An `io.Flusher` might be nice, but it's not really necessary. You can ju
by cdoxsey 8y ago
The whimsical nature of interfaces is definitely different coming from other languages. An `io.Flusher` might be nice, but it's not really necessary. You can just write:
type Interface interface {
io.Writer
Flush() error
}
func someFunction(w Interface) error {
w.Write(nil)
w.Flush()
panic("etc...")
}
Or even drop the type name:
func someFunction(w interface {
io.Writer
Flush() error
}) error {
w.Write(nil)
w.Flush()
panic("etc...")
}
But maybe that looks too weird.
- rbrtl 8y agoThis is something I'm still struggling to grok. The interface idioms feel so different to my Java instincts, like loose fitting clothes after years of skinny jeans.
- skj 8y agoBasically, the interfaces get defined where they're used, not where they're implemented.
- apta 8y agoGolang's interfaces are a half-baked feature, not so different from the rest of the language. What they basically were aiming for is called "structural typing". However, other languages that have that feature have a much better approach, without sacrificing readability, functionality, or discoverability, and ending up causing bugs in the standard library like golang did.