3 ms·
As many others have stated, there are options. In the linked list example, you could use: - container/list.List which uses its own Element with internal pointe
by robmccoll 8y ago
As many others have stated, there are options. In the linked list example, you could use:
- container/list.List which uses its own Element with internal pointers and interface{} for values
- write a type-specific implementation (not generic, but no runtime type assertion)
- write your own implementation using your own node / element interface type (maybe your list is generic to types that support io.Writer - generic for some use, but if you need access to specific types, you're back to runtime type assertions)
- write your own interface{} implementation (constantly needs runtime type assertions)
- use code generation (maintenance is harder, potentially non-standard tooling / build steps, but hey - no runtime type assertions)
- use a slice instead of a list (Go's version of vectors / dynamic arrays) - still requires some implementation, but the language has a lot of built-ins that make this easier. Depends on use. If you are doing a lot of insertions in the middle, this may suck, FIFO may suck and you need to be careful about not doing append()-shift and effectively leaking memory, LIFO this is dynamic and gives you good cache locality. Up shot is no-runtime type stuff and at least the slices themselves and the built-ins are generic.