Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Go support templates or generics?

Tags:

oop

generics

go

I know that Go doesn't have classes in the traditional OOP sense, but Go does provide a notion of interfaces that allows you do most of the OOP things you'd want to do.

BUT, does Go allow for something like creating a templated class? For instance, I'm reading through the source code for the container/list package. It defines a list and the list's associated methods. But in all methods, the values contained in the list are of type interface{} -- so, of any type. Is there any way to create a list that is constrained to only hold values of a particular type? int, string, Fruit... whatever.

like image 363
JnBrymn Avatar asked Dec 15 '13 20:12

JnBrymn


1 Answers

Newer than gotgo, there's a code-generation-based package called "gen".

http://clipperhouse.github.io/gen/

gen is an attempt to bring some generics-like functionality to Go, with inspiration from C#’s Linq, JavaScript’s Array methods and the underscore library. Operations include filtering, grouping, sorting and more.

The pattern is to pass func’s as you would pass lambdas in Linq or functions in JavaScript.

like image 190
Eve Freeman Avatar answered Sep 23 '22 18:09

Eve Freeman