Get started with generics in Go
Many programming languages have the concept of generic functions — code that can elegantly accept one of a range of types without needing to be specialized for each one, as long as those types all implement certain behaviors.Generics are big time-savers. If you have a generic function for, say, returning the sum of a collection of objects, you don’t need to write a different implementation for each type of object, as long as any of the types in question supports adding. [ Tune into Dev with Serdar to get Go coding tips from InfoWorld’s Serdar Yegulalp in 5 minutes or less ] When the Go language was first introduced, it did not have the concept of generics, as C++, Java, C#, Rust, and many other languages do. The closest thing Go had to generics was the concept of the interface, which allows different types to be treated the same as long as they support a certain set of behaviors.To read this article in full, please click here
Many programming languages have the concept of generic functions — code that can elegantly accept one of a range of types without needing to be specialized for each one, as long as those types all implement certain behaviors.
Generics are big time-savers. If you have a generic function for, say, returning the sum of a collection of objects, you don’t need to write a different implementation for each type of object, as long as any of the types in question supports adding.
When the Go language was first introduced, it did not have the concept of generics, as C++, Java, C#, Rust, and many other languages do. The closest thing Go had to generics was the concept of the interface, which allows different types to be treated the same as long as they support a certain set of behaviors.