Starting with version 1.18, Go has added support for generics, also known as type parameters. |
|
|
|
|
|
As an example of a generic function, |
|
As an example of a generic type, |
|
|
|
We can define methods on generic types just like we
do on regular types, but we have to keep the type
parameters in place. The type is |
|
AllElements returns all the List elements as a slice. In the next example we’ll see a more idiomatic way of iterating over all elements of custom types. |
|
|
|
When invoking generic functions, we can often rely
on type inference. Note that we don’t have to
specify the types for |
|
… though we could also specify them explicitly. |
|
|
|
Next example: Range over Iterators.