Go:Generics
lastmod 2025-11-20created 2025-11-201 edits
Generic function
A generic function is a function that can use parameters of various types within the function. In the Go language, you can write a generic function in the following form.
In the example code above, [T any] is a generic type parameter. T represents the type used within the function, and the any keyword means that all types are allowed. All parameters of type T used within the function are treated as the same type.
Generic type
A generic type is a type that can perform the same behavior across multiple types. In the Go language, you can write a generic type in the following form.
In the example code above, [T any] is a generic type parameter. T represents the type used in that type, and the any keyword means that all types are allowed. Using generic types, you can perform the same behavior across multiple types while guaranteeing type safety.