4 ms·
Whenever I see interface{} mentioned in Go I see vague references to polymorphism. That is wrong. Interface is a special type in Go. All interfaces in go, inc
by thatjamesdude 5y ago
Whenever I see interface{} mentioned in Go I see vague references to polymorphism.
That is wrong.
Interface is a special type in Go. All interfaces in go, including ones you declare, hold 2 pieces of data in memory
1. Pointer to the concrete type
2. Metadata about the concrete type
interface{} is not a special thing, it is just the empty interface, and everything satisfies the empty interface.
This is quite neat. All a type assertion does is follow the pointer and check the type data.
There is also a type switch in Go. https://play.golang.org/p/22aegLocHXI https://play.golang.org/p/22aegLocHXI
This makes building handlers around the dynamic type easy. But for the most part: Single function interfaces. Compose those to make make larger interfaces if absolutely needed (see io.Writer, io.Reader and io.ReadWriter)