3 ms·
There is certainly some overlap between those features, but I think they largely cover different usecases. For example, consider trying to implement a List dat
by zeugmasyllepsis 9y ago
There is certainly some overlap between those features, but I think they largely cover different usecases.
For example, consider trying to implement a List data structure in Go. What type does the List hold? It poses no constraints on the type of things put in it, except that they must all share that type.
The List can store `interface{}`, but then there's nothing to stop you from adding two different types to the list. And what type of value would a method like `getFirstItem()` return? Just an `interface{}`, forcing the user to cast.
Go's interfaces express constraints on individual types (e.g. type A has methods Foo and Bar) but not across functions defined on a struct (e.g. the type that a List stores is the SAME type that the getHead() function returns).