But since you have to use casts you are giving up some advantages of static typing, right?
Yes, but the `container/list` standard library uses the same approach.
I wrote a Cartesian product library that took a different tack--it splits the index and iterator cursor logic (integer fiddling) into one struct and the actual type into another, so it's easy to build type-checked objects.
In full disclosure, though, that was mainly because the alternative (using `interface{}`) was so utterly painful.
That sounds interesting and useful. Is that code publically available anywhere?
Sure. Say you have a Set type (which is custom, since Go doesn't provide a `container/set` type.
All of the methods (`Add()`, `Contains()`, etc.) are going to be declared with `interface{}` parameters, e.g., You would also want a method to get a slice of all members, e.g., If you want to use this as a set of strings, you would add without a cast: However, when pulling it out you would need to cast it to a string. So if you were converting `[]interface{}` to `[]string`, it would look like this: